Skip to Content
-1

How to get rightmost characters in a string after "-" in ABAP?

Jun 12, 2017 at 10:07 AM

318

avatar image

I have a field which contains text of employee subgroup. For Eg.

1. MD-Exec-MD

2. DR-Exec-Directors

From the above text I want the rightmost part of the text after 2nd "-". Can anyone suggest something. Idea will be appreciated.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

6 Answers

Richard Harper Jun 12, 2017 at 11:56 AM
3
l_Ds = SubString( Val = 'DR-Exec-Directors'
                  Off = Find( Val   = 'DR-Exec-Directors'
                              Regex = '-[^-]+$' ) + 1 ).
Share
10 |10000 characters needed characters left characters exceeded
Sandra Rossi Jun 12, 2017 at 07:34 PM
3
third_segment = segment( val = 'DR-Exec-Directors' index = 3 sep = '-' ).
ASSERT third_segment = 'Directors'.
Share
10 |10000 characters needed characters left characters exceeded
Mike Pokraka Jun 12, 2017 at 12:21 PM
3

Another options just for fun:

data(result) = match( val   = 'DR-Exec-Directors'  
                      regex = '[^-]*$' ).
Share
10 |10000 characters needed characters left characters exceeded
Raghu Govindarajan Jun 12, 2017 at 01:53 PM
2

While I prefer Mike Pokraka's and Richard Harper's answers - they will be better performance-wise - if you don't have a recent version of ABAP, here is an older approach.

DATA:
  substring_tab TYPE STANDARD TABLE OF string,
  last_substring TYPE string,
  num_lines TYPE i.

SPLIT 'DR-Exec-Directors' AT '-' INTO TABLE substring_tab IN CHARACTER MODE.
DESCRIBE TABLE substring_tab LINES num_lines.

READ TABLE substring_tab INTO last_substring INDEX num_lines.
Share
10 |10000 characters needed characters left characters exceeded
Supreeth M Gowda
Jun 12, 2017 at 10:45 AM
0

Hi Badal,

Check my code for u r requirement..

SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}

DATA id type STring VALUE 'DR-Exec-Directors'.



find  ALL OCCURRENCES OF '-' IN id IGNORING CASE  MATCH COUNT data(id1).

CHECK sy-subrc eq 0 and id1 = 2.

SPLIT id AT '-' INTO DATA(str1) DATA(str2) data(str3).



WRITE : 'The right most part of string',id,'is',':', str3 .
Show 3 Share
10 |10000 characters needed characters left characters exceeded

Thanks for the reply. It was very useful.

0

welcome man contribute as much u can :)

please do close if u got answer

0

This will work only if you have a fixed number of hyphens. What if you don't know how many you will have?

0
Anand Sagar Sethi Jun 12, 2017 at 01:54 PM
0

Hi

I will write as below:

which works perfectly:

Data a type string.

a =  'MD-Exec-MD'.

data: result_tab type  MATCH_RESULT_TAB.

find  ALL OCCURRENCES OF '-' in a

RESULTS result_tab.


the last line of result_tab will have the answer

Show 2 Share
10 |10000 characters needed characters left characters exceeded

You missed a couple of steps there. What you get is the position of the last hyphen, from there you need to determine what the last string is using that position + 1, up to the length of the string.

0

Thanks Raghu!

0