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

splitting a string value

Former Member
0 Likes
2,112

Dear Friends,

one of my string value is   "0021009010001000123NVN"

i have to split 123 ( before some alphabets )and want to store this value in another variable

other string can be like this also for eg 2225018000605010456PV

in the above string i have to split 456 and want to store this value in another variable

could anyone be pls give me some sample program

Thanks

Vijaya

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,076

Here is a one-liner way to do it using regular expressions.

For more information, you can read keyword documentation and explore DEMO_REGEX_TOY program.

See this snippet.

  1. DATA: str TYPE string,
  2.       value TYPE string.
  3. str = '0021009010001000123NVN'.
  4. FIND REGEX '([1-9]+)[a-zA-Z]+$' IN str SUBMATCHES value.
  5. IF sy-subrc EQ 0.
  6.   WRITE:/ value.
  7. ENDIF.
  8. str = '2225018000605010456PV'.
  9. FIND REGEX '([1-9]+)[a-zA-Z]+$' IN str SUBMATCHES value.
  10. IF sy-subrc EQ  0.
  11.   WRITE:/ value.
  12. ENDIF.

/.

13 REPLIES 13
Read only

Former Member
0 Likes
2,076

Hi, if this parts of string have fixed value, try something like this

str = '0021009010001000123NVN'

text01 = str(16) .

text02 = str+16(3) ..

text03 = str+19.

Read only

yogendra_bhaskar
Contributor
0 Likes
2,076

Hi Vijaya ,

use offset

data : str(20) VALUE '0021009010001000123NVN',

        var(3).

var = str+16(3).

WRITE : var.



Regards,


Yogendra

Read only

0 Likes
2,076

my string is not fixed , could any one pls let me know , how to remove alphabets from the string

Thanks

Read only

0 Likes
2,076

Hi ,

If you simply want to remove alphabets then assign the string to a numeric character variable.

It will only assign numbers to it.

data : str(20) VALUE '0021009010001000123NVN',

        var(3),

        num(20) TYPE n.

num = str.

WRITE : num.


Regards,


Yogendra

Read only

adrian_mejido
Contributor
0 Likes
2,076

Hi Vijaya,

Try with this code:

DATA: l_leng TYPE i,
       l_offset TYPE i,
       l_string1 TYPE string VALUE '0021009010001000123NVN',
       l_string2 TYPE STRING,
       l_string3 TYPE string,
       l_string4 TYPE string.


      l_leng = strlen( l_string1 ).



do.

   if l_offset = l_leng.
     exit.
   endif.

   if l_string1+l_offset(1) co '0123456789'.
     concatenate l_string2 l_string1+l_offset(1into l_string2.
   else.
*  The value is a LETTER
     concatenate l_string3 l_string1+l_offset(1into l_string3.
   endif.

   l_offset = l_offset + 1.

enddo.


l_leng = strlen( l_string2 ).

l_offset = l_leng - 3.

l_string4 = l_string2+l_offset(3).
l_string2 = l_string2(l_offset).

The result is this:

Best Regards

Read only

Former Member
0 Likes
2,076

Do something like thist.

DESCRIBE FIELD string LENGTH len IN CHARACTER MODE.

do.

if ofst = len1.

    exit.

  endif.

if str+ofst(1) co sy-abcde.

lv_alphabet_found = ofst.

endif.

ofst = ofst + 1.

enddo.

lv_alphabet = lv_alphabet_found - 4. '' This will give you the place from where you need three words

Now read like this

move  STRING+lv_alphabet(3) to desired variable

Read only

Former Member
0 Likes
2,076

Get the length of the string, (strlen);

use a index variable to loop

WHILE index < length

here you verify if the index of the string is not a number, if true, you will have when your split starts.

Read only

Former Member
0 Likes
2,076

Hi Laxmi,

Use below code

DATA: g_str TYPE string VALUE '0021009010001000123NVN',

       g_count TYPE i,

       g_var TYPE char30  .

* finding the first character position in string

FIND FIRST OCCURRENCE OF REGEX '[A-Z]' IN g_str MATCH OFFSET g_count.

g_count = g_count - 3.

WRITE g_str+g_count(3).


Note: You no need to write do... enddo or while...endwhile.


It will work.


Regards,

Vineesh

Read only

Former Member
0 Likes
2,077

Here is a one-liner way to do it using regular expressions.

For more information, you can read keyword documentation and explore DEMO_REGEX_TOY program.

See this snippet.

  1. DATA: str TYPE string,
  2.       value TYPE string.
  3. str = '0021009010001000123NVN'.
  4. FIND REGEX '([1-9]+)[a-zA-Z]+$' IN str SUBMATCHES value.
  5. IF sy-subrc EQ 0.
  6.   WRITE:/ value.
  7. ENDIF.
  8. str = '2225018000605010456PV'.
  9. FIND REGEX '([1-9]+)[a-zA-Z]+$' IN str SUBMATCHES value.
  10. IF sy-subrc EQ  0.
  11.   WRITE:/ value.
  12. ENDIF.

/.

Read only

0 Likes
2,076

Hi Manish,

I have never seen that expression, but It's very useful.

Despite that, I think that you solution will only work if there is at least one zero before last three numbers.

Is there any way to specify the number of bits that you want to get?

Best regards

Read only

0 Likes
2,076

Snippet works on 0021009010001asdf123NVN and 0021009010001asdf12345NVN too.

For second sample, you can restrict the match to only 3 digits using curly brackets. Same can be used to give upper and lower limits of length.

Try regex ([1-9]{3})[a-zA-Z]+$ on input string 0021009010001asdf12345NVN to get 345 as match.

Read only

0 Likes
2,076

Thanks a lot Manish, good work!!

Read only

Former Member
0 Likes
2,076

Hi,,

vineesh code works..

you can use it