Application Development 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: 

Find first space in text field

Former Member
0 Kudos

This must be easy, but I can't figure it out. I am trying to find the first space in a string field, and concatenate the string there. For example, if I have the string "AAAAA BBBBB", I want to capture only "AAAAA". I am trying to do a string search to find the first space in the field, but this does not seem to work. If I search for an actual character value, for example, using "" above, this works. This is an example of what I have tried:

FIND ' ' in 'AAAAA *BBBBB'.

This returns a value of 0 in sy-fdpos.

If I use this:

FIND '*' in 'AAAAA *BBBBB'.

it returns a value in sy-fdpos.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Unfortunately, it looks like SEARCH doesn't "find" space.

You have two options. If you want to just take the portion of the string that is before the first space, then you can do something like this.


DATA : v_string(20) VALUE 'AAAAA *BBBBB',
       v_first_half(10).

START-OF-SELECTION.

  SPLIT v_string AT space INTO v_first_half v_string .

  WRITE:/ v_first_half.

Or you replace space with some other character and then do the search for that, like here


DATA : v_string(20) VALUE 'AAAAA *BBBBB CCCCC',
       v_first_half(10).

START-OF-SELECTION.

  REPLACE space WITH '@' INTO v_string.
  SEARCH v_string FOR '@'.
  WRITE:/ sy-fdpos.
  v_first_half = v_string+0(sy-fdpos).
  WRITE:/ v_first_half.

7 REPLIES 7

Former Member
0 Kudos

Hi denise,

did you try with <b>SEARCH</b>

or you can use <b>split</b>

<b>SPLIT f AT g INTO h1 ... hn.</b>

Effect

Splits f wherever the separator g occurs and places the resulting sections into the fields h1 ... hn (n >= 2). Note that if g has type C, the field is used in its defined and not its occupied length.

The field is split using the following procedure: f is split internally into a set of target fields k1 to kn with the same type as f. These are then transferred into the actual target fields h1 to hn using MOVE semantics.

reward points for helpfull answers and close the thread if your question is solved.

regards,

venu.

0 Kudos

Try this code. The system does not like it when you search for blank spaces, fill them with some character, in this case "$". Then search for it.

[code]

report zrich_0001.

data: s type string.

data: new_val type string.

s = 'AAAAA *BBBBB'.

replace space with '$' into s.

search s for '$'.

check sy-fdpos <> 0.

new_val = s(sy-fdpos).

write new_val.

[/code]

Regards,

Rich Heilman

0 Kudos

Yes, I tried that too, and that didn't work:


search lv_charMatnr for ' '

Former Member
0 Kudos

Try:


REPORT ztest NO STANDARD PAGE HEADING LINE-SIZE 255.

data: wa(25) value 'AAAAA *BBBBB',

      begin of itab occurs 0,
        field(25),
      end of itab.

split wa at space into table itab.

Your value is in the first line.

Rob

Message was edited by: Rob Burbank

Former Member
0 Kudos

Unfortunately, it looks like SEARCH doesn't "find" space.

You have two options. If you want to just take the portion of the string that is before the first space, then you can do something like this.


DATA : v_string(20) VALUE 'AAAAA *BBBBB',
       v_first_half(10).

START-OF-SELECTION.

  SPLIT v_string AT space INTO v_first_half v_string .

  WRITE:/ v_first_half.

Or you replace space with some other character and then do the search for that, like here


DATA : v_string(20) VALUE 'AAAAA *BBBBB CCCCC',
       v_first_half(10).

START-OF-SELECTION.

  REPLACE space WITH '@' INTO v_string.
  SEARCH v_string FOR '@'.
  WRITE:/ sy-fdpos.
  v_first_half = v_string+0(sy-fdpos).
  WRITE:/ v_first_half.

Former Member
0 Kudos

Hi Denise

Try this

SEARCH <string> FOR ' '

here <string> is the variable name of the string

this will return you the position of the character found in the system varaible SY-FDPOS and Sy-SUBRC 0 if found.

Former Member
0 Kudos

Denise

One way of doing this is

data: v_string type string,

v_concat_string type string,

v_char type c,

v_strlen type i,

v_index type i.

V_STRING = 'AAAAA *BBBBB cccc'.

v_concat_string = 'xxxx'.

v_strlen = strlen( v_string ).

do.

v_index = sy-index - 1.

if v_index >= v_strlen.

exit.

endif.

v_char = v_string+v_index(1).

if v_char = ' '.

concatenate v_string+0(v_index) v_concat_string into v_string.

exit.

endif.

enddo.

write: / v_string.

Let me know if it works

KK