07-10-2023 2:58 PM
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.
07-10-2023 3:16 PM
07-10-2023 3:04 PM
For example I wrote it "Joe Smith" but I get this error: Please enter your first and last names separated by a space.
07-10-2023 3:16 PM
07-11-2023 8:21 AM
07-11-2023 12:36 PM
Shorter:
IF contains( val = p_name pcre = '\s' ).
(but maybe it's too much overhead using PCRE for just a space?)
07-11-2023 10:38 AM
07-11-2023 12:45 PM
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.