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

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'
注意:+兩邊不能有空格。

posted on 2016-10-25 10:37  seven1314pp  阅读(6836)  评论(0编辑  收藏  举报