‎2005 Jul 14 9:32 AM
Hi all,
Is there any function to reverse a string ? I also want to know whether there is any function that takes a string as input and returns nth word of that string.
regards,
Vishnu Priya.
‎2005 Jul 14 10:09 AM
one way is to use an internal table for the string.....
to get the nth word, split your string into an internal table and then read the nth entry of the table..
split str at ' ' into table itab.
now suppose for 4th word,
read table itab index 4.
this will give you the 4th word in the string...
if u dont want to use an internal table then u can also use explicit variables for splitting...
split str at ' ' into f1 f2....fn.
and then use the respective result field
regards,
PJ
‎2005 Jul 14 9:36 AM
Hello Vishnu Priya,
To find the nth word of the string, use STRLEN statement.
To reverse a string, use the function module STRING_REVERSE.
Regards
Kalyan
‎2005 Jul 14 9:41 AM
Hi Priya,
Thanks for ur very quick reply. STRLEN will return the length of a string. But I want to find nth word in a string. For Eg. if string is ' This is a sample string'. I want to get 4th word in that string, i.e, 'sample'. Is there any such function. I will try STRING_REVERSE. Thank u.
regards,
Vishnu Priya
‎2005 Jul 14 9:43 AM
Hi,
REPORT demo_data_string.
shifting strings by a given number of places
DATA: t1(10) TYPE c VALUE 'abcdefghij',
string1 LIKE t1.
string1 = t1.
WRITE string1.
SHIFT string1.
WRITE / string1.
string1 = t1.
SHIFT string1 BY 3 PLACES LEFT.
WRITE / string1.
string1 = t1.
SHIFT string1 BY 3 PLACES RIGHT.
WRITE / string1.
string1 = t1.
SHIFT string1 BY 3 PLACES CIRCULAR.
WRITE / string1.
SKIP.
ULINE.
shifting up to a given string
DATA: t2(10) TYPE c VALUE 'abcdefghij',
string2 LIKE t2,
str2(2) TYPE c VALUE 'ef'.
string2 = t2.
WRITE string2.
SHIFT string2 UP TO str2.
WRITE / string2.
string2 = t2.
SHIFT string2 UP TO str2 LEFT.
WRITE / string2.
string2 = t2.
SHIFT string2 UP TO str2 RIGHT.
WRITE / string2.
string2 = t2.
SHIFT string2 UP TO str2 CIRCULAR.
WRITE / string2.
SKIP.
ULINE.
shifting a string depending on the first or last sign
DATA: t3(14) TYPE c VALUE ' abcdefghij',
string3 LIKE t3,
str3(6) TYPE c VALUE 'ghijkl'.
string3 = t3.
WRITE string3.
SHIFT string3 LEFT DELETING LEADING space.
WRITE / string3.
string3 = t3.
SHIFT string3 RIGHT DELETING TRAILING str3.
WRITE / string3.
SKIP.
ULINE.
replacing values
DATA: t4(10) TYPE c VALUE 'abcdefghij',
string4 LIKE t4,
str41(4) TYPE c VALUE 'cdef',
str42(4) TYPE c VALUE 'klmn',
str43(2) TYPE c VALUE 'kl',
str44(6) TYPE c VALUE 'klmnop',
len4 TYPE i VALUE 2.
string4 = t4.
WRITE string4.
REPLACE str41 WITH str42 INTO string4.
WRITE / string4.
string4 = t4.
REPLACE str41 WITH str42 INTO string4 LENGTH len4.
WRITE / string4.
string4 = t4.
REPLACE str41 WITH str43 INTO string4.
WRITE / string4.
string4 = t4.
REPLACE str41 WITH str44 INTO string4.
WRITE / string4.
SKIP.
ULINE.
translating signs
DATA: t5(10) TYPE c VALUE 'AbCdEfGhIj',
string5 LIKE t5,
rule5(20) TYPE c VALUE 'AxbXCydYEzfZ'.
string5 = t5.
WRITE string5.
TRANSLATE string5 TO UPPER CASE. "#EC SYNTCHAR
WRITE / string5.
string5 = t5.
TRANSLATE string5 TO LOWER CASE. "#EC SYNTCHAR
WRITE / string5.
string5 = t5.
TRANSLATE string5 USING rule5. "#EC SYNTCHAR
WRITE / string5.
SKIP.
ULINE.
overlaying strings
DATA: t6(10) TYPE c VALUE 'a c e g i ',
string6 LIKE t6,
over6(10) TYPE c VALUE 'ABCDEFGHIJ',
str6(2) TYPE c VALUE 'ai'.
string6 = t6.
WRITE string6.
WRITE / over6.
OVERLAY string6 WITH over6.
WRITE / string6.
string6 = t6.
OVERLAY string6 WITH over6 ONLY str6.
WRITE / string6.
SKIP.
ULINE.
<b>
*searching strings
DATA string7(30) TYPE c VALUE 'This is a little sentence.'.
WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'.
ULINE /1(26).
SEARCH string7 FOR 'X'.
WRITE: / 'X', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string7 FOR 'itt '.
WRITE: / 'itt ', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string7 FOR '.e .'.
WRITE: / '.e .', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string7 FOR '*e'.
WRITE: / '*e ', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SEARCH string7 FOR 's*'.
WRITE: / 's* ', sy-subrc UNDER 'SY-SUBRC',
sy-fdpos UNDER 'SY-FDPOS'.
SKIP.
ULINE.</b>*
DATA: string8(30) TYPE c VALUE 'This is a fast first example.',
pos8 TYPE i,
off8 TYPE i.
WRITE / string8.
SEARCH string8 FOR 'ft' ABBREVIATED.
WRITE: / 'SY-FDPOS:', sy-fdpos.
pos8 = sy-fdpos + 2.
SEARCH string8 FOR 'ft' ABBREVIATED STARTING AT pos8 AND MARK.
WRITE / string8.
WRITE: / 'SY-FDPOS:', sy-fdpos.
off8 = pos8 + sy-fdpos - 1.
WRITE: / 'Off:', off8.
SKIP.
ULINE.
length of a string
DATA: int TYPE i,
word1(20) TYPE c VALUE '12345',
word2(20) TYPE c ,
word3(20) TYPE c VALUE ' 4 '.
int = strlen( word1 ). WRITE int.
int = strlen( word2 ). WRITE / int.
int = strlen( word3 ). WRITE / int.
SKIP.
ULINE.
condensing strings
DATA: string9(25) TYPE c VALUE ' one two three four',
len9 TYPE i.
len9 = strlen( string9 ).
WRITE: string9, '!'.
WRITE: / 'Length: ', len9.
CONDENSE string9.
len9 = strlen( string9 ).
WRITE: string9, '!'.
WRITE: / 'Length: ', len9.
CONDENSE string9 NO-GAPS.
len9 = strlen( string9 ).
WRITE: string9, '!'.
WRITE: / 'Length: ', len9.
SKIP.
ULINE.
concatenating strings
DATA: c1(10) TYPE c VALUE 'Sum',
c2(3) TYPE c VALUE 'mer',
c3(5) TYPE c VALUE 'holi ',
c4(10) TYPE c VALUE 'day',
c5(30) TYPE c ,
sep(3) TYPE c VALUE ' - '.
CONCATENATE c1 c2 c3 c4 INTO c5.
WRITE c5.
CONCATENATE c1 c2 c3 c4 INTO c5 SEPARATED BY sep.
WRITE / c5.
SKIP.
ULINE.
splitting strings
DATA: string10(60) TYPE c ,
p1(20) TYPE c VALUE '++++++++++++++++++++',
p2(20) TYPE c VALUE '++++++++++++++++++++',
p3(20) TYPE c VALUE '++++++++++++++++++++',
p4(20) TYPE c VALUE '++++++++++++++++++++',
del10(3) TYPE c VALUE '***'.
string10 = ' Part 1 *** Part 2 *** Part 3 *** Part 4 *** Part 5'.
WRITE string10.
SPLIT string10 AT del10 INTO p1 p2 p3 p4.
WRITE / p1.
WRITE / p2.
WRITE / p3.
WRITE / p4.
SKIP.
ULINE.
moving parts of strings
DATA: mc1(10) TYPE c VALUE 'ABCDEFGHIJ',
mc2(10) TYPE c .
MOVE mc1 TO mc2 PERCENTAGE 40.
WRITE mc2.
MOVE mc1 TO mc2 PERCENTAGE 40 RIGHT.
WRITE / mc2.
Hope this helps.
Thanks & Regards,
Judith.
‎2005 Jul 14 10:09 AM
one way is to use an internal table for the string.....
to get the nth word, split your string into an internal table and then read the nth entry of the table..
split str at ' ' into table itab.
now suppose for 4th word,
read table itab index 4.
this will give you the 4th word in the string...
if u dont want to use an internal table then u can also use explicit variables for splitting...
split str at ' ' into f1 f2....fn.
and then use the respective result field
regards,
PJ
‎2005 Jul 14 10:56 AM
‎2006 Aug 22 12:30 PM
Things are always easier than people imagined. Just a hint for getting nth word of a string:
data: chr type sychar01,
str type string,
nth type i value 5,
len type i value 1.
chr = str+nth(len).
write: / chr.
See what will happen.