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: 

Get last letter of a string....

Former Member
0 Kudos
29,184

Hi guys.

Have a problem with ABAP in SAP.

I need to get the last character of the string, like this...

'AAAAAAAA BBBB 1 QQQQQQQQ 2'.

i need to take the last letter (2) to do more things b4.

I'm thinking to rotate the string and take the first letter, but i think will exist a FM that do my requirement.

See you.

Thanks: Miguel

1 ACCEPTED SOLUTION

Former Member
0 Kudos
4,657

Hi,

use below logic

str = 'test'.

data a type i.

a = strlen(str).

write str(a).

Regards

Amole

10 REPLIES 10

Former Member
4,657

name = 'PRABHU'.

n = strlen(name).

n = n - 1. (n = 5)

output = name+n(1) = U.

Regards

Prabhu

Former Member
0 Kudos
4,657

one long way is

first use STRING_LENGTH FM , then subtract 1 from length

then use STRING_SPLIT_AT_POSITION and pass the length previously calculated

Former Member
0 Kudos
4,657

use the below code.

data : l_length type i,

l_data(50).

l_data = 'AAAAAAAA BBBB 1 QQQQQQQQ 2'.

l_length = STRLEN( l_data ) . " here u will get the length of the field l_data.

by using the length read your variable l_data

and get the last 2 records

former_member588853
Active Contributor
0 Kudos
4,657

Hi,

First get the string lengthof the variable.

data: lv_string type char4 value 'ABCD',

lv_new type i,

lv_new1 type char2.

*lv_new = strlen(lv_string).

CALL FUNCTION 'STRING_LENGTH'

EXPORTING

string = lv_string

IMPORTING

LENGTH = lv_new

.

lv_new = lv_new - 2.

lv_new1 = lv_string+lv_new(2).

write lv_new1.

You get the last two characters.

reward if useful..

regards,

nazeer

Message was edited by:

nazeer shaik

former_member189631
Active Contributor
0 Kudos
4,657

Miguel,

One way to do this is ,

  • find the string length by usinf strlen(strVariableName or String) command.

  • Find the string lenght-1 posotion of that string.

Regards,

<b>Ramganesan K</b>

Former Member
0 Kudos
4,658

Hi,

use below logic

str = 'test'.

data a type i.

a = strlen(str).

write str(a).

Regards

Amole

0 Kudos
4,657

Lots of ways to do this. another one would be to use the function module REVERSE_STRING, or STRING_REVERSE. It is something like that. Anyway, from the name, you can guess what it does, then all you need to do is look at the first letter of the string using an offset.

REgards,

RIch Heilman

0 Kudos
4,657

GREAT, i love that approaches of you rich!

0 Kudos
4,657

thanks everybody for your responses.

i like a lot the Amole solution.

thanks anyway.

see you SAPers

4,657

Hi, the top answer is not correct at all. If you don't mind the longer read, my blog post compares all the solutions from this page: https://blogs.sap.com/2020/01/18/how-to-retrieve-the-last-character-of-a-string-in-abap-definitive-e...