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

Picking the first word of a sentence

I have a string where I have to pick out the first word. for eg " hello world". I have to take HELLO into another variable.Is there any abap keyword for doing that??

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    Posted on Dec 19, 2008 at 11:37 AM

    you can split the sentence into an internal table and read the first row:

    SPLIT string AT space INTO TABLE itab.
    READ TABLE itab INDEX 1.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 11:49 AM

    Check this code snippet.

    DATA: text TYPE string VALUE 'Nitesh Kumar'..
    DATA: BEGIN OF itab OCCURS 0,
      str1(10) TYPE c,
      str2(10) TYPE c,
      END OF itab.
    SPLIT text AT space INTO TABLE itab.
    READ TABLE itab INDEX 1.
    WRITE itab-str1.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 11:50 AM

    Hi Priya,

    You can get the first word of a string using the following logic,

    "Data Section
    DATA: w_var TYPE string VALUE 'Hello world',
              w_pos TYPE i.
    
    "Logic for the first word
    IF w_var CA space.
      w_pos = sy-fdpos. "Getting offset
    ENDIF.
    
    w_var = w_var+0(w_pos). "Getting the first string
    
    WRITE: / w_var.

    Hope this will help you.

    Regards,

    Manoj Kumar P

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 12:00 PM

    Hi,

    You can use SPLIT statement.Please find below code.

    data: v_text type char100,

    v_text1 type char100,

    v_text2 type char100.

    v_text = 'HELLO WORLD'.

    split v_text at space into v_text1 v_text2.

    First word will be in variable v_text1.

    Hope this may be helpful.

    Regards,

    Sravanthi

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 19, 2008 at 12:02 PM

    Hi,

    Try this...

    data : val1 type string value 'Hello World',

    val2 type string,

    val3 type string.

    split val1 at '' into val2 val3.

    write : val2.

    val2 will contain first word (Hello) and val3 will contain the remaining word in the sentence (World).

    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.