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 last text from a string?

Former Member
0 Kudos

Hello,

I require last text from below eg:

field1 = text1|text2|text3.

field2 = text1|text2|text3|text4.

field3 = text1|text2|text3|text4|text5..

how can i get only text3 from field1.....text4 from field2.

Regards,

Rachel

1 ACCEPTED SOLUTION

former_member209217
Active Contributor
0 Kudos

Hi,

Are there any field separators or delimiters used in ur fields .If so,you can split and then extract the desired text from ur field.

split field1 at <delimiter> into text1 text2 text3

Alternatively by knowing string length and using offset value you can be able to get the texts from the fields.

regards,

Lakshman.

9 REPLIES 9

former_member209217
Active Contributor
0 Kudos

Hi,

Are there any field separators or delimiters used in ur fields .If so,you can split and then extract the desired text from ur field.

split field1 at <delimiter> into text1 text2 text3

Alternatively by knowing string length and using offset value you can be able to get the texts from the fields.

regards,

Lakshman.

Former Member
0 Kudos

Hi

If you know the length of the text, then its easy. Suppose the length of the last text is 10 char. Then following things to be done.

Data: l_n type i.

l_n = strlen (field1) - 10.

last text = field1+l_n(10).

Please check if this works.

Thanks

Sourav

Former Member
0 Kudos

Hello

If '|' is delimiter then:


data: begin of itab,
      text(20),
      end of itab.
data: n type i.
split field1 at '|' into table itab.
describe table itab lines n.
read table itab index n.
write itab-text.

... etc for field2, field 3 .....

kesavadas_thekkillath
Active Contributor
0 Kudos

if there is a seperator the go for this


  data:input type string.
  data:itab type table of char10.
  data:wa like line of itab.
  input = 'text1|text2|text3'.
  split input at '|' into TABLE itab.
  sort itab DESCENDING.
  read table itab index 1 INTO wa.
  write wa.
  

rainer_hbenthal
Active Contributor
0 Kudos

> I require last text from below eg:

> how can i get only text3 from field1.....text4 from field2.

So, what is your problem? To get the last text or to get the third text?

SuhaSaha
Advisor
Advisor
0 Kudos

Hello Rachel,

A small variation of Keshu's solution:

DATA:
ITAB TYPE STANDARD TABLE OF STRING,
V_OUTPUT TYPE STRING,
V_LINES TYPE I.

SPLIT V_INPUT AT '|' INTO TABLE ITAB.

V_LINES = LINES( ITAB ).

READ TABLE ITAB INTO V_OUTPUT INDEX V_LINES.

BR,

Suhas

venkat_o
Active Contributor
0 Kudos

Hi, <li>Try this way.


report ztest.
data:field type string VALUE 'text1|text2|text3|text4|text5'.
DATA:last_field type string.
DATA:it_data type TABLE OF string.
DATA:wa_data LIKE LINE OF it_Data.

SPLIT field at '|' INTO TABLE it_data.
DESCRIBE TABLE it_data LINES sy-tfill.
READ TABLE it_data INTO wa_data INDEX sy-tfill.
last_field = wa_data.
WRITE last_field.
Thanks Venkat.O

I355602
Advisor
Advisor
0 Kudos

Hi,

You have:-

field1 = text1|text2|text3.

field2 = text1|text2|text3|text4.

field3 = text1|text2|text3|text4|text5.

Refer:-


DATA : v_lines TYPE i.
DATA : itab TYPE STANDARD TABLE OF string.
DATA : wa LIKE LINE OF itab.

SPLIT field1 AT '|' INTO TABLE itab.

DESCRIBE TABLE itab
LINES v_lines.

"read last text
READ TABLE itab INTO wa INDEX v_lines.
IF sy-subrc = 0.
  "now you have the last text in work area
ENDIF.

Similarly you can use field2 and field3 to get the last text.

Hope this helps you.

Regards,

Tarun

Former Member
0 Kudos

Thanks for the quick response & resolving my query.