Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Abap logic

ravi_chandra3
Active Contributor
0 Likes
2,853

Hi Experts,

I have small query,

Let's say i have data in notepad as

abc,"sunil,kumar",100,"praveen,kumar".

xyz,"sateesh,kumar",200,"sukesh,kumar".

Now my requirement is , i want to replace the , with + symbol but the , in the (double quotes)  should remain as it is.

My Expected output is :

abc+"sunil,kumar"+100+"praveen,kumar".

xyz+"sateesh,kumar"+200+"sukesh,kumar".

Hope u got my question.

Regards,

RaviChandra.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,757

This will do the job.

If each line is separated by commas and ends with a period:

REPLACE ALL OCCURENCES OF REGEX ',(?![^"]*"(,|\.))' IN lt_text WITH '+'.

If the periods are not at the end of the line:

REPLACE ALL OCCURENCES OF REGEX ',(?![^"]*"(,|$))' IN lt_text WITH '+'.

24 REPLIES 24
Read only

Former Member
0 Likes
2,757

You can use REPLACE command.

It's very simple. Please, see its sintax on the internet.

Regards.

Vinícius Ostan

Read only

Former Member
0 Likes
2,757

Hi Ravi,

First Take the Note pad content into the characters, then Use the command Replace for those you want to do.

Read only

0 Likes
2,757

Hi all

I have used the replace statement but it is converting all the , into + .

I have done this already but not getting.

Please once check the requirement clearly.

Read only

Former Member
0 Likes
2,757
Hi Ravi Chandra,
I guess your doubt remains in the fact that if you REPLACE the commas with '+', even the ones within the quotes would get replaced. If that is your concern, then the simple solution is to REPLACE all ," with +"... then only the commas preceding open quotes would be replaced by +.
REPLACE ALL OCCURENCES OF ',"' IN <YOUR STRING> WITH '+"'.
Hope this helps.
Please Note: This can only work if all your data is within quotes.
Regards,
Subhrangsu
Read only

0 Likes
2,757

Hi bagchi,

Thanx for the reply,

But in my requirement i have get the data with some numbers like

abc+"sunil,kumar"+100+"praveen,kumar",250,200,Hyderabad,Andra Pradesh,India

But for the above string it won't work.

Read only

0 Likes
2,757

Hi Ravi,

You need an indicator that needs to be unique.

Otherwise, you won't identify which character needs to be changed or not.

Can't you talk with your function about it?

Best regards.

Read only

0 Likes
2,757

Hi

Do u have any idea regarding how to extract the data which is present in double quotes ,

Let say in the below string.

abc+"sunil,kumar"+100+"praveen,kumar".

i want to extract  sunil,kumar and praveen,kumar .

Is there any possibility to achieve this.

Regards,

RaviChandra.

Read only

0 Likes
2,757

Can we try with split Statement, Will it work?

Splitting Character Strings (SAP Library - ABAP Programming (BC-ABA)) 

For reference use the upper link..

I thing it will work. Split your string from +.

Read only

0 Likes
2,757

Try something like below:

data: string(50) type c,

symbol1(1) type c,

symbol2(1) type c,

symbol3(1) type c,

part1(10)  type c,

part2(10)  type c,

part3(10)  type c,

part4(10)  type c.

symbol1 = '"'.

symbol2 = ','.

symbol3 = '+'.

string = 'abc,"sunil,kumar",100,"praveen,kumar".'.

SPLIT string AT symbol INTO part1 part2 part3 part4.

concatenate symbol1 part1 symbol1 into part1.

replace all occurrences of symbol2 in part2.

concatenate symbol1 part3 symbol1into part3.

replace all occurrences of symbol2 in part4.

concatenate part1 part2 part3 part4 into string separated by symbol3

Read only

0 Likes
2,757

Hi Shekar

I tried with Split statement but it is not getting.

Regards,

RaviChandra

Read only

0 Likes
2,757

Look Gave code using Split statement only.. Try that ... Its looks cool and useful too.

Read only

0 Likes
2,757

Hi Jeevan,

Thanx for the reply,

I think the code will work if the string is of format which is given by me,

If the string format is different then the code need to be changed .

the string may differ with , and " , " .

The above is just a sample record i have given,

I think we can do it by using the wild card search.

Pleaase can any body let me know how to store the '"*,*"' into one variable using search statement .

Pleaase let me know.

Regads,

RaviChandra.

Read only

0 Likes
2,757

Hi Ravi,

Please copy/paste the code below, run it on debug mode then see the it_string2 table, there you'll find your value separated correctly in each line.

DATA string(50).

DATA string2(50).

DATA p1 TYPE sy-index.

FIELD-SYMBOLS <fs_string> TYPE any.

DATA it_string TYPE TABLE OF string.

DATA it_string2 TYPE TABLE OF string.

APPEND 'abc,"sunil,kumar",100,"praveen,kumar".' TO it_string.

LOOP AT it_string INTO string.

   DO .

     p1 = sy-index - 1.

     ASSIGN string+p1(1) TO <fs_string>.

     IF <fs_string> IS ASSIGNED.

       IF <fs_string> EQ '"' .

         DO.

           IF sy-index EQ 1.

             CLEAR <fs_string>.

             CONTINUE.

           ELSEIF <fs_string> EQ '"'.

             APPEND string2 TO it_string2.

             CLEAR string2. CLEAR <fs_string>.

             CONTINUE.

           ENDIF.

           CONCATENATE string2 <fs_string> INTO string2.

           p1 = p1 + 1.

           ASSIGN string+p1(1) TO <fs_string>.

           IF <fs_string> IS NOT ASSIGNED.

             EXIT.

           ENDIF.

         ENDDO.

       ELSE.

         CLEAR <fs_string>.

       ENDIF.

     ELSE.

       EXIT.

     ENDIF.

   ENDDO.

ENDLOOP.

BREAK-POINT.

Regards,

I hope it can help you.

Regards.

Read only

Former Member
0 Likes
2,757

Hi Ravi,

I have not tested this code.

But you can use this logic.

string2(50)

string1 = abc,"sunil,kumar",100,"praveen,kumar".

length = strlen( string1 ).

int i.

do.

   if i GT length.   " Do only till the length of string.

        exit.

    endif.

   if string1+i(1) = ' " ' .   "If an open quote comes, copy till the closing quote.

      do.

        concatenate string2 string1+i(1) into string2.

             if string1+i(1) = ' " ' .

                  exit.

             endif.

          i = i+1.

       enddo.

    elseif string1+i(1) = ' , ' .     "If its a comma, replace it with a '+'

            concatenate string2 '+' into string2.

            i = i+1.

    else.

            concatenate string2 string1+i(1) into string2.   " Else just copy that character to the string.

            i = i+1

      endif.

  enddo.     

    

        

Let me know if it gives the required output.

Regards,

Susmitha

Read only

0 Likes
2,757

Hi susmitha,,

Thanx for the reply,

I have tried with ur code, it is not satisfying for some of the records

do u have any other apporach, i think wild card search will satisfy my requirement,

Do u have any idea on how to store the wild card search in one variable,like i want to store '"*"' in one variable and then want to replace the , with space and then concatenate the string in the same location.

Hope u got my quesiton.

Thanx again.

Read only

0 Likes
2,757

Can you give an example of the records that the above logic didn't satisfy?

Read only

0 Likes
2,757

Hi

Can you explain with the wild card search. I will paste the records once i login to system again.

I have a records which has length of 1200 ( In the flat file i have fields for 200 ) so i think we can have some performance impact ( not sure about it) but i heard that wild card search will satisfy the requirement.

Please express ur view on it

Thanx alot again.

Read only

0 Likes
2,757

Check this link.

http://help.sap.com/SAPHELP_470/Helpdata/EN/fc/eb33cc358411d1829f0000e829fbfe/content.htm

Search for *pattern gives a word ending with pattern and <pattern>* gives a word beginning with pattern.

And words are separated by space, comma, periods, semicolon etc.

So search for "* gives the position of the start of string in *, and search for *" gives the position of the end of string.

I hope this was what you wanted.

Read only

0 Likes
2,757

Ravi Chandra wrote:

i heard that wild card search will satisfy the requirement.

Where did you hear that? How do you think a wild card would satisfy your requirement?

Imagine if you search *,* in this even ",n or k," or even something like <200 characters>,<300 characters> will satisfy the condition and how are you gonna specify the length your wild card.Also system will not return the part of the string to you, it's just gonna set values to some system variables like sy-subrc.

Read only

0 Likes
2,757

Hi jeevan,

Thanx for the reply,

I am just thinking wild card may satisfy my requirement , i m not sure whether it will satisfy or not. Unless i get some input.

Will update if i get any input

Regards,

RaviChandra.

Read only

Former Member
0 Likes
2,758

This will do the job.

If each line is separated by commas and ends with a period:

REPLACE ALL OCCURENCES OF REGEX ',(?![^"]*"(,|\.))' IN lt_text WITH '+'.

If the periods are not at the end of the line:

REPLACE ALL OCCURENCES OF REGEX ',(?![^"]*"(,|$))' IN lt_text WITH '+'.

Read only

0 Likes
2,757

Hi Eric,

Thanx alot...

It work pretty Good...


Regards,

RaviChandra.

Read only

Former Member
0 Likes
2,757

Hi,

This may be a little unconventional, but...

Save the file as a comma delimeted text file (i.e. just save the notepad .txt file)

Open the text file in Excel as a comma delimited text file.

Save the file in Excel as a tab delimited text file.

Now you can replace the tabs with "+" and not destroy the commas within the names.

Good Luck.

Regards,

mr. kim

Read only

Former Member
0 Likes
2,757

Ravi,

I've tested the code above and it works fine to me.

Please, let me know if it can help you.

Regards.

Vinicius Ostan