Application Development and Automation 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: 
Read only

remove last character in a string

Former Member
0 Likes
30,931

I have a variable with C type for example lv_c = 'xxxx#'. Now I want to remove the last character of this variable value? which function do I use? and how do I do that. Thanks!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
13,447

Hi,

data w_i type i.

w_i = strlen( lv_c ).

subtract 1 from w_i.

write : lv_c+0(w_i).

This is working.

14 REPLIES 14
Read only

Former Member
0 Likes
13,447

use: SHIFT lv_c.

Read only

Former Member
0 Likes
13,447

Hi,

just use offsetting..

data

w_temp like lv_c.

lv_c = 'xxxx#'.

w_temp = lv_c+0(4).

write: w_temp.

output is like this:

XXXX

lv_c+0(4) means it takes the first(i.e.from starting) 4 characters.based on this u can omitt or select the characters..just mention the offset value...

Regards

Kiran

Edited by: Kiran Saka on Feb 10, 2009 8:31 AM

Read only

Former Member
0 Likes
13,447

hi,

u can use n = strlen(lv_c) // u will get length of the string..

then lv_c = lv_c+0(n-1)... will fetch u the value except last character..

Rgds.,

subash

Read only

former_member222860
Active Contributor
0 Likes
13,447

Hi,

here's the logic.

data: str type string value 'abcd#',    len type i.

len = strlen( str ).
len = len - 1.
str = str+0(len).

write:/ str.

Read only

0 Likes
13,447

thank u issue resolved.

Read only

Former Member
0 Likes
13,447

first find string length from function strlen

lv_len = strlen(lv_c)

lv_len = lv_len - 1

then make one more var of type lv_c lv_c1

lv_c1 = lv_c + 0(lv_len)

Read only

Former Member
0 Likes
13,448

Hi,

data w_i type i.

w_i = strlen( lv_c ).

subtract 1 from w_i.

write : lv_c+0(w_i).

This is working.

Read only

Former Member
0 Likes
13,447

Hi ,

You can use the following code It will solve the problem.-----

DATA : w_data(6) TYPE c VALUE 'ABCDEF'.
CONDENSE w_data NO-GAPS.
SHIFT  w_data RIGHT .
WRITE w_data.

Regards

Pinaki

Read only

viquar_iqbal
Active Contributor
0 Likes
13,447

Hi

you can use

shift lv_c

condense lv_c

it should give you the remaining characters

Thanks

Viquar Iqbal

Read only

Former Member
0 Likes
13,447

Dear,

It is a special character.

To remove the special characters use below FM,

CONVERT_STREAM_TO_ITF_TEXT

Thanks and Regards,

Read only

Former Member
0 Likes
13,447

hi,

data:

w_str(5) type c value 'xxxx#'.

shift w_str by 1 places right.

write:/ w_str.

regards,

Mdi.Deeba

Read only

Former Member
0 Likes
13,447

Hi Anthony ,

Kindly go through this link below:

http://help.sap.com/saphelp_nw70/helpdata/en/9f/db999535c111d1829f0000e829fbfe/content.htm

Hope it helps

Regrds

Mansi

Read only

Former Member
0 Likes
13,447

Hi,

You can write this logic,

DATA:  lv_len TYPE i.
lv_len = STRLEN( lv_c ).
SUBTRACT 1 FROM lv_len.
lv_c = lv_c+0(lv_len). "Here last character will be removed

Regards,

Manoj Kumar P

Read only

AkshayAnil919
Explorer
0 Likes
4,979

While working in ABAP, especially when preparing data for file downloads or exporting internal tables, you might notice that some lines seem to end with a " # " symbol. But here's the catch  that " # " is not always a real hash symbol. Naturally, you might think it’s a hashtag â€” but often, it’s not. These characters don’t display visually, but depending on how the file is opened or downloaded, they may appear as strange or misleading symbols.

What’s Actually Happening?

Sometimes, that " # " is just how non-printable characters like carriage return (CR) or newline (LF) appear when viewed in SAP ALV, Excel, or when downloading.
These hidden characters:

  • Can break the line unexpectedly in the downloaded file
  • May cause the data to appear in a new row or line
  • Often sneak in from copied text, manual input, or external systems

How to Fix It? - Solution (with hex check)

Here’s a reliable way to detect the actual last character and remove it only if needed (e.g. it's a # or control character):

DATA: lv_value    TYPE string VALUE 'ABCDE#',  " Input string (may have unwanted char at end)" replace this variable with yours*
      lv_len      TYPE i,                      " Length of the string
      lv_lastval  TYPE string,             " Last character of the string
      lv_hex      TYPE xstring,                 " Hex value of the last character
      lv_newval   TYPE string.                 " Final cleaned value

" Step 1: Find the length of the string
lv_len = strlen( lv_value ).
SUBTRACT 1 FROM lv_len.  " Adjust to point to last character (0-based index)

" Step 2: Get the last character
lv_lastval = lv_value+lv_len(1).

" Step 3: Convert last character to hex using built-in function module
CALL FUNCTION 'NLS_STRING_CONVERT_FROM_SYS'
  EXPORTING
    lang_used = sy-langu       " Use system language
    source    = lv_lastval     " Character to convert
    to_fe     = 'MS '          " Target frontend encoding (Microsoft)
  IMPORTING
    result    = lv_hex         " Hex value of the character
  EXCEPTIONS
    OTHERS    = 1.             " Simple error catch

" Step 4: Check if the character is '#' (hex 23)
IF lv_hex = '23'.  " ASCII hex for '#'
  lv_newval = lv_value+0(lv_len).  " Remove last character
ELSE.
  lv_newval = lv_value.            " Keep original if not '#'
ENDIF.
  • NLS_STRING_CONVERT_FROM_SYS helps get the true hex value of a character.
  • This avoids misleading visual characters (like #) that are actually CR, LF, etc.

I hope this helps! Let me know your thoughts or if you have any questions.
Thanks,

Akshay Anil 
Technical Consultant, SAP ABAP | BTP | Fiori | Ui5