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

help with substring

sap_cohort
Active Contributor
0 Likes
1,200

Hi, I have some text fields I need to parse and could use some help.  The fields I have to parse have an ID followed by a space and then more text.

I would like to capture the left most text/id before the first space only.  I know with all the new abap functions there must be a quick function to accomplish this.  I tried one of the substr functions but it didn't like using SPACE as a separator.

Thanks for any help

Example: 

Field TextResult Value
AB1 Reason Code 1 AB1
AB2 Reason Code 3AB2
AA Reason Code 3AA
AB Reason Code 4AB
1266 Reason Code NA1266
18222 Reason Code High18222
1 ACCEPTED SOLUTION
Read only

sap_cohort
Active Contributor
0 Likes
1,167

I got this working using SPLIT, but I have to output to 2 target fields even though I only need the first one.  Anyone know of any of the newer functions like substring might also do the trick?

6 REPLIES 6
Read only

sap_cohort
Active Contributor
0 Likes
1,168

I got this working using SPLIT, but I have to output to 2 target fields even though I only need the first one.  Anyone know of any of the newer functions like substring might also do the trick?

Read only

former_member300076
Participant
0 Likes
1,167

Hi Kenneth,

Try this.

DATA: lv_space          TYPE xfeld VALUE IS INITIAL,
            lv_postn          TYPE i VALUE IS INITIAL,
            lv_char1          TYPE char1,
            lv_subst          TYPE char5 VALUE IS INITIAL. "Choose the max. number of char you need


WHILE lv_space NE 'X'.
   CLEAR: lv_char1.

   lv_char1                        = your_string+lv_postn(1).

   IF lv_char1 EQ space.    "Check if space
     lv_space                      = 'X'.
   ELSE.
     CONCATENATE lv_subst lv_char1 INTO lv_subst.
   ENDIF.

   lv_postn                        = lv_postn + 1.
ENDWHILE.

Regards,

Sam,

Read only

VenkatRamesh_V
Active Contributor
0 Likes
1,167

Hi,

Try.

DATA lv_text type string value 'AB1 Reason Code 1'.

DATA: itab type table of string,

       wa   like line of itab.

SPLIT lv_text at ' ' into table itab.

READ TABLE itab into wa  INDEX 1.

WRITE wa.

Hope it helpful,

Regars,

Venkat.

Read only

0 Likes
1,167

This is one I did not think of although it might be more processing intensive.

Thanks for the reply back.

Read only

hitesh_gabani
Participant
0 Likes
1,167

Hi,

Try below code it works for me.

DATA: lv_street TYPE string,

           lv_off    TYPE i.

lv_street = 'AB1 Reason Code 1'.

FIND FIRST OCCURRENCE OF REGEX '\s' IN lv_street MATCH OFFSET lv_off.

lv_street = lv_street(lv_off).

WRITE:/ lv_street.


Output : AB1



Regards,

Hitesh

Read only

hitesh_gabani
Participant
0 Likes
1,167

Hi Kenneth,

If your problem solved then mark answer as Helpful or Correct.

Regards,

Hitesh