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: 

how to get 2nd value of a string

Former Member
0 Kudos

Hi,

if there is string which is divide into 2 parts i.e. string1 and string2. how can i get the 2nd value of 2nd string.

plzz provide me guidelines for it.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Ricx,

the following code works perfectly,

'-' is not inserted at space, also if length is 0/ GT/ LT the stringlength .. chk this..



   data:
I_STRING TYPE  STRING value 'system appli',
LENGTH TYPE  I value 4,
E_STRING TYPE  STRING.

data: STRING_F type string, "Stores the value in string format
      STRING_LENGTH type i, "Length of the string
      DIFF type i,          "Difference among the value
      DIFF1 TYPE C,
      STRING1 type string,  "Stores the 1st String
      STRING2 type string,  "Stores the 2nd String
*      STRING3 type string,  "Stores the 3rd String
*      STRING4 type string,  "Stores the 3rd String
       string3 type c,
       string4 type c,
      STRING5 type string,  "Stores the 3rd String
      string6 type string,
      LENGTH2 type I.

STRING_F = I_STRING.
STRING_LENGTH = STRLEN( I_STRING ).
DIFF = STRING_LENGTH - LENGTH.

IF DIFF LE 0.
  DIFF = 0.
ENDIF.

if length ge 0.

IF LENGTH LE STRING_LENGTH.
  STRING1 = STRING_F(LENGTH).
ELSE.
  STRING1 = STRING_F(STRING_LENGTH).
ENDIF.

IF LENGTH LE STRING_LENGTH.
  STRING2 = STRING_F+LENGTH(DIFF).
ELSE.
  STRING2 = STRING_F+STRING_LENGTH(DIFF).
ENDIF.

length2 = length - 1.
if ( length2 lt string_length )
and ( length2 ge 0 ).
STRING3 = STRING1+length2(1).
endif.

if string2 ne space.
STRING4 = STRING2+0(1).
endif.

IF ( LENGTH LE STRING_LENGTH )
AND ( STRING3 Na space )
AND ( STRING4 Na space ).
  concatenate STRING1 '-' STRING2  into STRING5.
  e_string = STRING5.
  write:/ e_string.
ELSE.
  concatenate  STRING1 STRING2 into STRING5.
  e_string = STRING5.
    write:/ e_string.
ENDIF.

else.
e_string = STRING_F.
write:/ e_string.
endif.

Regards,

Mdi.Deeba

19 REPLIES 19

Former Member
0 Kudos

Hi,

can u please send your code for better understanding of the issue

0 Kudos

hi,

let me show you the example.

if the string is :- 'THIS IS DEMO'

total length = 12.

i want to insert - in it say at 6th position and i pass the value to the 6 position.

now it is divided into to strings say string1 and string 2.

string1 = 'THIS ' and string2 = 'is demo'

according to the passed value the highfen will be inserted in the 6th position i.e. I but i want that if there is a character at 7th position and there is a space in the 5 th position then the insretion of the highfen should not take place.Now i want to know how do i get the 2nd value of the 2nd string?

Plzzz provide me guidlines to solve this problem.

Sm1tje
Active Contributor
0 Kudos

Sounds like a SPLIT statement to me.


SPLIT xxxx AT '?'

Former Member
0 Kudos

First split the string into 2 parts string1 and string2 by using the SPLIT keyword.

Now using the string2 we can get the second character as

char2 = string2+1(1).

This will provide the 2nd character of the 2nd string.

Former Member
0 Kudos

string = string2+1(1).

Former Member
0 Kudos
how can i get the 2nd value of 2nd string.

do u mean second character of second string?

code.

split string into string1 string2.

write:/ string2

or write 😕 string2+1(1) (only second character)

Regards

faisal_altaf2
Active Contributor
0 Kudos

Hi, Ricx

Test The following Code i think you need this.

DATA: string TYPE string VALUE 'value1 string2',
      st1 TYPE string,
      st2 TYPE string.

SPLIT string AT '' INTO st1 st2.

WRITE: '2nd Value of 2nd String is =', st2+1(1).

Please Reply if else Requirement.

Best Regards,

Faisal

former_member555112
Active Contributor
0 Kudos

Hi,

You can use the SPLIT statement.

Regards,

Ankur Parab

Former Member
0 Kudos

Hi,

try using FM STRING_SPLIT

Where provide a string with delimiter as some char that seperates the string.

eg. String HELLO-WORLD

Delimiter as -

Output

HEAD HELLO

TAIL: WORLD

in abap pro display the TAIL value.

hope this helps!!

thanks

ravi aswani

0 Kudos

hi,

i had already made the FM for it which is working fine but the problem occurs according to the passed value the highfen will be inserted in the 6th position i.e. I but i want that if there is a character at 7th position and there is a space in the 5 th position then the insretion of the highfen should not take place.

0 Kudos

Hi,

if string6(1) is not initial and string4(1) is initial.

write code for not insertion of the hifen.

endif.

0 Kudos

hi,

i had tried that logic also but it is still not giing the desired result.

0 Kudos

Hi, Ricx.

Test Following Sample Code hope will solve out your problem,

DATA: string TYPE c LENGTH 20 VALUE 'THIS IS DEMO'.

WRITE: / '5th Position = ', string+4(1), / '7th Position = ', string+6(1).
IF string+4(1) EQ '' AND string+6(1) NE ''.
* Do nothing
ELSE.
  string+5(1) = '-'.
ENDIF.

WRITE: / string.

Please Reply if any Issue,

Best Regards,

Faisal

0 Kudos

Hi,

please check the code below.

if string6(1) is not initial and string4(1) is initial.

else.

string+5(1) = '-'.

split string at '-' into string1 string2.

endif.

write string.

Former Member
0 Kudos

Hi,

Try this, hope it will solve ur problem for getting the value of 2nd string.

x(3) type c.

strn = ''aaaa aaa aaa''

x = strlen( strn ) .

if strn+0(6) = ' ' .

strn2 = strn+6(x).

endif.

Former Member
0 Kudos

Can we use spilt statement?

Former Member
0 Kudos

Hi,

In my last post i missed one step so, Try this, hope it will solve ur problem for getting the value of 2nd string.

x(3) type c.

strn = ''aaaa aaa aaa''

x = strlen( strn ) .

if strn+0(6) = ' ' .

x = x - 6.

strn2 = strn+6(x).

endif.

Former Member
0 Kudos

Hi Ricx,

the following code works perfectly,

'-' is not inserted at space, also if length is 0/ GT/ LT the stringlength .. chk this..



   data:
I_STRING TYPE  STRING value 'system appli',
LENGTH TYPE  I value 4,
E_STRING TYPE  STRING.

data: STRING_F type string, "Stores the value in string format
      STRING_LENGTH type i, "Length of the string
      DIFF type i,          "Difference among the value
      DIFF1 TYPE C,
      STRING1 type string,  "Stores the 1st String
      STRING2 type string,  "Stores the 2nd String
*      STRING3 type string,  "Stores the 3rd String
*      STRING4 type string,  "Stores the 3rd String
       string3 type c,
       string4 type c,
      STRING5 type string,  "Stores the 3rd String
      string6 type string,
      LENGTH2 type I.

STRING_F = I_STRING.
STRING_LENGTH = STRLEN( I_STRING ).
DIFF = STRING_LENGTH - LENGTH.

IF DIFF LE 0.
  DIFF = 0.
ENDIF.

if length ge 0.

IF LENGTH LE STRING_LENGTH.
  STRING1 = STRING_F(LENGTH).
ELSE.
  STRING1 = STRING_F(STRING_LENGTH).
ENDIF.

IF LENGTH LE STRING_LENGTH.
  STRING2 = STRING_F+LENGTH(DIFF).
ELSE.
  STRING2 = STRING_F+STRING_LENGTH(DIFF).
ENDIF.

length2 = length - 1.
if ( length2 lt string_length )
and ( length2 ge 0 ).
STRING3 = STRING1+length2(1).
endif.

if string2 ne space.
STRING4 = STRING2+0(1).
endif.

IF ( LENGTH LE STRING_LENGTH )
AND ( STRING3 Na space )
AND ( STRING4 Na space ).
  concatenate STRING1 '-' STRING2  into STRING5.
  e_string = STRING5.
  write:/ e_string.
ELSE.
  concatenate  STRING1 STRING2 into STRING5.
  e_string = STRING5.
    write:/ e_string.
ENDIF.

else.
e_string = STRING_F.
write:/ e_string.
endif.

Regards,

Mdi.Deeba

Former Member
0 Kudos

Hi,

Try this following code to get the string2 as per ur requirement..Hope it will solve ur problem....

data: x(2) type c ,str(30) type c VALUE 'aaaaaa oaaaaau uuuuuuuu'.

x = strlen( str ).

if str+6(1) = ''.

x = x - 7.

str = str+7(x).

WRITE / str.

endif.