ABAP字符串操作 截取字符长度 取位数

  • A+
所属分类:Notes

ABAP字符串操作
ABAP對字串的操作方法與其他語言的操作有較大差別,以下是較常用的對字串操作的方法:
1. 字串的連接:CONCATENATE
DATA: t1 TYPE c LENGTH 10 VALUE 'We',
t2 TYPE c LENGTH 10 VALUE 'have',
t3 TYPE c LENGTH 10 VALUE 'all',
t4 TYPE c LENGTH 10 VALUE 'the',
t5 TYPE c LENGTH 10 VALUE 'time',
t6 TYPE c LENGTH 10 VALUE 'in',
t7 TYPE c LENGTH 10 VALUE 'the',
t8 TYPE c LENGTH 10 VALUE 'world',
result TYPE string.
CONCATENATE t1 t2 t3 t4 t5 t6 t7 t8 INTO result. "直接連接
CONCATENATE t1 t2 t3 t4 t5 t6 t7 t8 INTO result SEPARATED BY space. "用空格將每個字串連接

2. 拆分字串: SPLIT
DATA: str1 TYPE string,
str2 TYPE string,
str3 TYPE string,
itab TYPE TABLE OF string,
text TYPE string.

text = What a drag it is getting old.
SPLIT text AT space INTO: str1 str2 str3,
TABLE itab.
3. 去除多余的空格:CONDENSE
DATA: BEGIN OF sentence,
word1 TYPE c LENGTH 30 VALUE 'She',
word2 TYPE c LENGTH 30 VALUE 'feeds',
word3 TYPE c LENGTH 30 VALUE 'you',
word4 TYPE c LENGTH 30 VALUE 'tea',
word5 TYPE c LENGTH 30 VALUE 'and',
word6 TYPE c LENGTH 30 VALUE 'oranges',
END OF sentence,
text TYPE string.
text = sentence.
CONDENSE text.“在每個連接串中間會有一個空格
CONDENSE text No-GAPS. "在後面加入NO-GAPS後,所有空格都會去除。
4.字串轉換:TRANSLATE
DATA text TYPE string.
text = Careful with that Axe, Eugene.
TRANSLATE text TO UPPER CASE. "轉大寫
TRANSLATE text TO LOWER CASE. "轉小寫
DATA text TYPE string.
text = Barbcbdbarb.
TRANSLATE text USING 'ABBAabba'. “A與B換轉 a與b換轉
5.取字串長度:STRLEN
data text type c length 20.
data: ilen type i.
text = Barbcbdbarb.
ilen = strlen( text ). "字串實際長度(11)
describe field text length ilen in byte mode. "字串定義時的字節長度,一個長度等2個字節(40)
describe field text length ilen in character mode. "字串定義的長度(20)
6.截取子字串
data: text type c length 10.
data: ilen type i.
data: subtext type c length 5.
text = 'ABCDEFGHIJ'.
subtext = text+0(5). "取左邊5個字符('ABCDE')
subtext = text+3(5). "從第四個字串起取5個字符('DEFGH)
text+0(1) = 'Z'. "將第一個字符變爲'Z'
注意:+兩邊不能有空格。

http://space.itpub.net/17204926/viewspace-609916

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  1   博主  0

    • ag

      DATA LV_STRING TYPE STRING.

      DATA LV_CHAR1 TYPE CHAR35.
      DATA LV_CHAR2 TYPE CHAR35.
      DATA LV_CHAR3 TYPE CHAR35.

      LV_STRING = ‘杰夫·贝索斯现在是世界上最富有的人,他的演讲中提到一个概念 『与人为善,比聪明更重要』,很多人的聪明是与生俱来的,做一个聪明的人并不难,与人为善却很难得,因为这要求你时刻注意自己的言语,设身处地为他人着想。’.

      LV_CHAR1 = LV_STRING+0(30).
      LV_CHAR2 = LV_STRING+30(30).
      LV_CHAR3 = LV_STRING+60(30).