ABAP字串操作 擷取字元長度 取位數

SAP騎士發表於2009-07-22
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'
注意:+兩邊不能有空格。   

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17204926/viewspace-609916/,如需轉載,請註明出處,否則將追究法律責任。

相關文章