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: 

ABAP first name and last name

0 Kudos

I need a code that has a parameter wich ask for first name and last name the should be splited by a space. I want it to give an error when someone give only on word or more than two words. I would like the output be like:

Firstname: Peter

Lastname: Molyneux

I have written this code but it does not work properly because even when I give it two separate names like my first name and last name it gives me the error that I am expecting when I give the wrong data.

REPORT Z_FIRSTNAME_LASTNAME.

PARAMETERS: p_name TYPE string LOWER CASE.

DATA: lv_first_name TYPE string,
      lv_last_name  TYPE string,
      lv_error_msg  TYPE string.

START-OF-SELECTION.

  IF p_name CO ' '.
    SPLIT p_name AT ' ' INTO lv_first_name lv_last_name.

    IF lv_first_name IS NOT INITIAL AND lv_last_name IS NOT INITIAL.
      WRITE: 'First Name:', lv_first_name.
      WRITE: / 'Last Name:', lv_last_name.
    ELSE.
      lv_error_msg = 'Please enter both first and last names.'.
    ENDIF.
  ELSE.
    lv_error_msg = 'Please enter your first and last names separated by a space.'.
  ENDIF.

  IF lv_error_msg IS NOT INITIAL.
    WRITE: / lv_error_msg.
  ENDIF.
1 ACCEPTED SOLUTION

raymond_giuseppi
Active Contributor

Try

IF p_name CS ` `.

6 REPLIES 6

0 Kudos

For example I wrote it "Joe Smith" but I get this error: Please enter your first and last names separated by a space.

raymond_giuseppi
Active Contributor

Try

IF p_name CS ` `.

jasmin_gruschke
Product and Topic Expert
Product and Topic Expert
 IF find( val = p_name pcre = '\s' ) NE -1.

Shorter:

IF contains( val = p_name pcre = '\s' ).

(but maybe it's too much overhead using PCRE for just a space?)

matt
Active Contributor
0 Kudos

Read the documentation. CO means "Contains Only".

Sandra_Rossi
Active Contributor
0 Kudos
IF pname CO ' '.

CO means CONTAINS ONLY all characters at the right (CONTAINS ONLY spaces).

But

IF pname CA ` `.

or

IF pname CS ` `.

CA means CONTAINS ANY (at least one) of the characters at the right (CONTAINS at least one space).

CS means CONTAINS SUBSTRING at the right (CONTAINS space).

In ABAP, it's very important to write text literals with backquotes when the last character is a space.

ABAP Keyword Documentation (sap.com)