Skip to Content
author's profile photo Former Member
Former Member

Find first space in text field

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    Posted on Oct 20, 2005 at 05:28 PM

    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.
    

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2005 at 05:17 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2005 at 05:26 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2005 at 05:31 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 20, 2005 at 05:54 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.