简体   繁体   English

从字符串中删除/提取最后一个字符

[英]Delete/extract last character from the string

Of course, question was discussed thousand times ( 1 , 2 , 3 ) but nothing was offered except this ugly snippet:当然,问题被讨论了一千次( 123 ),但除了这个丑陋的片段外没有提供任何东西:

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

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

Is there any elegant one-liner to do this?有没有优雅的单线来做到这一点? The only prominent way I found so far is到目前为止我发现的唯一突出的方法是

SHIFT str RIGHT DELETING TRAILING `,`.

However, it requires that you know what the last char is (TRAILING mask) and mask doesn't support regexp or wildcards.但是,它要求您知道最后一个字符是什么(TRAILING 掩码)并且掩码不支持正则表达式或通配符。 Or I am wrong?还是我错了?

This variant doesn't work for me for some reason由于某种原因,此变体对我不起作用

SHIFT string RIGHT BY 1.

Maybe somebody knows more beautiful syntax to do this in one line?也许有人知道在一行中执行此操作的更漂亮的语法? Anything new in ABAP 7.40 or 7.50? ABAP 7.40 或 7.50 中有什么新内容吗?

SUBSTRING to get the last character of a string: SUBSTRING 获取字符串的最后一个字符:

DATA: str TYPE string VALUE 'abcd#'.
str = substring( val = str off = strlen( str ) - 1 len = 1 ).

str will be '#'海峡将是 '#'

To remove the last character of a string (like in your example):要删除字符串的最后一个字符(如您的示例):

str = substring( val = str off = 0 len = strlen( str ) - 1 ).

Type in SUBSTRING in your ABAP Editor and hit F1 on it, there are some more variations (substring_after, substring_before, etc.)在您的 ABAP 编辑器中输入 SUBSTRING 并点击 F1,还有一些变体(substring_after、substring_before 等)

A trick I've used in the past:我过去用过的一个技巧:

DATA l_str TYPE string VALUE `abcd#`.

SHIFT l_str RIGHT by 1 PLACES CIRCULAR. " move last char to start, read it from l_str(1)
l_str = l_str+1. " remove it.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM