ABAP processing strings

Source: Internet
Author: User

The first character index of the string is 0; character fields: C, N, D, T, string (cndt => CN data time)

1. String connection
Concatenate dobj1 dobj2... into result
[In {byte | character} mode]
[Separated by SEP].

2. String separation. Split the part of a string to an inner table or a series of variables.
Split dobj at Sep
{Result1 result2...} | {table result_tab }}
[In {byte | character} mode].

3. string SEARCH: Find or search)
Find sub_string
In section [offset off] [length Len] of dobj --> the gray part is used to narrow the search range of the target string.
[In {byte | character} mode]
[{Respecting | ignoring} case]
[Match offset Moff] [Match length mlen].

Find 'dones'
In section offset 5 of 'Everybody knows this is nowhere'
Match offset Moff "=> Moff = 10
Match length mlen. "=> mlen = 5

Search for pattern in the string dobj
Search dobj for pattern [in {byte | character} mode]
[Starting at P1] [ending at P2]
[Abbreviated]
[And Mark].
If sy-subrc = 0. Then SY-FDPOS = returns the position of pattern in dobj
About pattern:
'Pat '-ignore trailing Spaces
'. Pat.'-do not ignore trailing Spaces
'* Pat'-end with Pat
'Pat * '-start with Pat
A word refers to a space ,;:? ! ()/+ = Separator string

4. String replacement
Replace section [offset off] [length Len]
Of dobj with new
[In {byte | character} mode].

Replace [{First Occurrence} | {all occurrences} of]
[Substring] sub_string
In [Section [offset off] [length Len] of] dobj with new
[In {byte | character} mode]
[{Respecting | ignoring} case]
[Replacement count rcnt]
[Replacement offset roff]
[Replacement length rlen].
Eg:
Data: text1 type string value 'xababx '.
Replace 'AB' in text1 with 'xx'. --> xxxabx

5. Remove leading zero)
Shift dobj Left deleting leading '0 '.
FM: conversion_exit_alpha_output

Add leading zero)
Data v_s (5 ).
Unpack '000000' to v_s. ==> v_s = '000000'
FM: conversion_exit_alpha_input

Shift dobj
[{By num places} | {up to sub_string}]
[Left | right] [Circular]
Shift dobj
{Left deleting leading} | {right deleting trailing} pattern.
[In {byte | character} mode].

6. String Length, number of rows in the table
Strlen (dobj) String Length
Number of rows in the Table in lines (itab)

7. Delete spaces in the string: condense text [no-gaps].

8. Case and character conversion
Translate text {to {upper | Lower} case} | {Using Pattern }.
Eg: text = 'barbcbdbarb '.
Translate text using 'abababba'. => 'abracadaba'

9. Convert
Convert date dat [Time Tim [Daylight Saving Time DST]
Into time stamp time_stamp Time Zone tz.

Convert time stamp time_stamp Time Zone TZ
Into [date dat] [Time Tim]
[Daylight Saving Time DST].

Convert text into sortable code hex.

10. Overlay text1 with text2 [only pattern].
If only pattern is not specified, spaces in text1 will be replaced by corresponding characters in text2.
If only matching characters are specified, they will be replaced. Note that the case sensitivity is true.

11. Pattern Matching
CO/CN contains only or not
Ca/Na contains any or not any
CS/ns contain string or not
CP/NP contains pattern or not

Note:
A). CO, NO, Ca, and Na are case sensitive and the trailing space is within the range of comparison.
Data: S1 (10) value 'aab '.
If S1 Co 'AB' => false
If S1 Co 'AB' => true
CS, NS, CP, and NP are case insensitive and trailing Spaces

B). For CP, NP
* = \ S?
+ = \ S
# Code-changing characters used to match characters such as * and +
##
#*
# +
# ___ Comparison end space
# [A-Z] It must be case sensitive in CP and NP

C) after the comparison, if the result is true, Sy-fdpos returns the offset of S2 in S1.

12. special characters
Add a newline or Tab character to the string, and use the ASCII code $13 $10 in other languages for insertion. however, the SAP class cl_abap_char_utilities should be used in the ABAP. there are character constants: cr_lf, horizontal_tab, newline, and so on.

13. String bit operation
Data: v_s (10) value 'abc '.
V_s + 0 (1) = 'B '.
V_s + 2 (*) = '12 '.
=> V_s = 'bb12 '.
========================================================== ================================
========================================================== ================================
The ABAP has a large difference between the string operation method and other statement operations. The following is a common string operation method:
1. String concatenation: 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. "directly connect
Concatenate T1 T2 T3 T4 T5 T6 T7 T8 into result separated by space. "concatenate each string with spaces

2. Split string: 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. Remove unnecessary spaces: 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. "There will be a space in each string.
Condense Text No-gaps. "After no-gaps is added to the backend, all spaces are removed.
4. String: Translate
Data Text Type string.
TEXT = 'careful with that axe, Eugene '.
Translate text to upper case. "large numbers
Translate text to lower case. "Small rows
Data Text Type string.
TEXT = 'barbcbdbarb '.
Translate text using 'ababbaabba'. "A and B should contain a and B should contain
5. Take the string length: strlen
Data Text Type C length 20.
Data: ilen type I.
TEXT = 'barbcbdbarb '.
Ilen = strlen (text). "Real length of the string (11)
Describe field text length ilen in byte mode. "The number of characters in the string at the time of definition. The number of characters in a length equals 2 to 40)
Describe field text length ilen in character mode. "length of the string definition (20)
6. Intercept the substring
Data: Text Type C length 10.
Data: ilen type I.
Data: subtext Type C Length 5.
TEXT = 'abcdefghj '.
Subtext = text + 0 (5). "5 characters ('abcde') on the left ')
Subtext = text + 3 (5). "5 characters ('defgh') from the fourth string)
Text + 0 (1) = 'Z'. "Change the first character to 'Z'
Note: + two partitions cannot have spaces.
Report z12.01.
Data string7 (30) Type C value 'abcdefghijklmnopqrstuvwxy '.
Write:/'searched', 'sy-subrc', 'sy-fdpos '.
Uline/1 (26 ).
Search string7 for 'x '.
Write:/'x', Sy-subrc under 'sy-subrc ',
Sy-fdpos under 'sy-fdpos '.
Write:/'look at the data here '.

1). Shift: truncates a string.
Shift {c} [by {n} Places] [{mode}].:
Purpose: remove the first n characters of the string. If n is not specified, the default value is 1. If the specified N is less than or equal to 0, the string remains unchanged. If N exceeds the length of the string, the string is null. Therefore, pay attention to n when you perform this operation. You can obtain the length of the string by Len = strlen (c ).
Mode: Specifies the direction of string truncation.
Left: Truncated from the left
Right: truncation from the right
Circular: place the character on the left to the right.
Shift {c} Up to {STR} {mode }.:
Purpose: Remove all the characters in the string before 'str'. You can also specify the mode. The principle is the same as above.
Shift {c} Left deleting leading {STR }.
Shift {c} Right deleting trailing {STR }.:
Purpose: remove the (left) or (right) character STR before the string C.
Common scenarios for the preceding statements:
A. Remove the leading characters from the string. For example, if ALV determines that a field is a 10-character with so, Po, and other numbers in it, and you do not want to display the preceding 0, you can do this: shift {c} Left deleting leading 0.
B. If you know that a string contains a fixed character and want to delete the character before the fixed character, you can use Shift {c} Up to {STR }. For example, an item adds a prefix to mga00002 and mga00003 by enhancing the prefix. The prefix is the data obtained in a fixed value table. In the specific display, remove the prefix. If you want to remove te In the TE-MRP-MAT1, MRP indicates that the material is running MRP, the requirement requires that the Te-remove, MRP can be obtained in the fixed value table, you can use Shift {c} Up To 'mrp '.
2). Replace: replace string
Replace {str1} with {str2} into {c} [length {L}].:
Purpose: replace str1 in string C with str2. If l is specified, the length of replacement is specified. If the replacement is successful, the SY-SUBRC is set to 0.
Common scenarios:
String replacement is commonly used. Note that the length of L is specified.
3). Translate: the conversion string.
Translate {c} to upper case.
Translate {c} to lower case .:
Function: converts the case sensitivity of a string.
Translate {c} using {r }.:
Purpose: Convert string C according to Rule R
Common scenarios:
SAP systems generally use uppercase letters, but some specific fields are marked with lowercase letters. When operating these fields, you must note the case conversion. Conversion rules are not commonly used, and I have not met them yet.
4). Search: search strings
Search {c} For {STR} {options }.:
Purpose: Look up STR in string C. If found, the SY-SUBRC is 0, and the SY-FDPOS is the specific position of the string.
Note: Use of the Mode
Specify STR: search for Str. Ignore the Space following Str
Specify. Str.: Search for STR, which contains spaces at the end of Str.
Specify * STR: Search for strings ending with Str
Specify Str *: Search for strings starting with Str
{Options:
It mainly uses starting at {N1} to specify the start position.
Ending at {N2} specifies the end position
Common scenarios:
It is generally used to determine whether a string meets the conditions. You can also use other statements to operate strings.
5). Condense: Remove spaces from the string.
Condense {c} [no-gaps].:
Purpose: remove the leading and trailing spaces in the string. If no-gaps is specified, remove all spaces in the string.
Common Use Case: obtain the exact length of a string for determination.
6). Split: Split string
Split {c} At {del} into {C1}... {CN }.
Purpose: Split string C into C1 according to the delimiters... CN.
Split {c} At {del} into table {itab }.
Purpose: Split C by the delimiters and place the corresponding fields in the inner table.
Common scenarios:
The file name is separated by the complete file path and the file name.
Difficulty: the number of splits cannot be determined.
Solution: Split them by two to the last one. Example: Str = C: "dir1" dir2 "dir3" File
Split STR at '"' into str1 str2.
Find str2 '/'.
Check sy-subrc = 0.
Do.
Find str2 '/'.
If sy-subrc = 0.
Split str2 into str1 str2.
Else.
Exit.
Endif.
Enddo.
The type of file upload is string, which is separated and put into the internal table. For example:
Data: Begin of itab occurs 0,
Col1 (30) type C,
End of itab.
Split STR at '"'into table itab.
Describe table itab lines.
Read Table itab index line.
Itab-col1 is File
7). concatenate: connection string
Concatenate {C1}... {CN} into {c} [separated by {s}].
Purpose: Set C1... CN is connected to C using S
Common scenarios: download files and edit fields in files.
8) shift <C> [by <n> places] [<mode>].
Move a string by the given number of locations
This statement moves <C> <n> fields. If by <n> places is omitted, <n> is interpreted as a position. If <n> is 0 or a negative value, <C> the value remains unchanged. If the length of <n> exceeds <C>, <C> enter a space. <N> can be a variable.

For different (<mode>) options, you can move the field <C>:. left: Move to the left <n> position, and fill the right with <n> spaces (default ). B. Right: Move right to the <n> position, and fill the left with <n> spaces. C. Circular: Move to the left <n> position so that <n> characters on the left appear on the right.
Move a field to a specified string
Shift <C> up to <STR> <mode>.
ABAP/4 searches for the <C> Field content until the <STR> string is found and moves the <C> field to the edge of the field. <Mode> the options are the same as those described in moving field strings by given positions. <STR> it can be a variable.
If <C> cannot find <STR>, set the SY-SUBRC to 4 without moving <C>. Otherwise, set SY-SUBRC to 0.
Move the field string based on the first or last character
If the first or last character meets certain conditions, the shift statement can be used to move the field to the left or right. To do this, use the following syntax:
Shift <C> Left deleting leading <STR>.
Shift <C> right deleting trailing <STR>.
If the first character on the left or the last character on the right appears in <STR>, this statement moves the field <C> to the left or right. Fill the right or left of the field with spaces. <STR> it can be a variable.

9). convert to a sorting format
Character fields can be converted into alphabetic formats:
Syntax
Convert text <C> into sortable Code <SC>.
This statement is used to fill the sortable target field with the character field <C> <SC>. The field <C> must be type C and the field <SC> must be Type X. The minimum length is 16 times the length of <C>.
The purpose of this statement is to <C> Create a relevant field <SC> for the character field as the sort keyword in alphabetical order of <C>. Sort the content of the inner table and the data that has been decompressed (see sort the inner table and sort the extracted dataset ).

If unconverted character fields are sorted, the system creates the order corresponding to the internal encoding of each letter on a specific platform. After sorting the target fields, convert text to create the target fields in this way, and sort the corresponding character fields in alphabetical order. For example, in German, the order is 'miller, Moller, M? Miller, muller', instead of 'miller, Moller,
Muller, M? Ler '.

The conversion method depends on the text environment that runs the ABAP/4 program. The text environment is defined in the user master record. With the exception, you can use the following statement to set the text environment in the program:

Syntax
Set locale language <LG> [country <Cy>] [modifier <m>].
This statement sets the text environment according to the language <LG>. For the country option, you can specify a country outside the language as long as the language of the country is different. For the modifier option, as long as one country
You can specify another identifier in different languages. For example, the sorting order is different between the phone book and the dictionary.
The fields <LG>, <Cy>, and <m> must be of type C and must be the same length as the key segment of table tcp0c. Table tcp0c is a table from which the text environment related to the platform is maintained. During the setlocale statement, the system sets the text Environment Based on entries in tcp0c. In addition to the platform features of internal transmission, you can use the set statement to specify the table keywords. If <LG> is equal to space, the system sets the text environment based on the user's master record. If no entry exists for the specified keyword in the table, a running error occurs.

The text environment affects all operations dependent on character sets in ABAP/4.
For more information about this topic, see the convert text and set locale language keyword documentation.
For an example of how to sort by language, see sort by internal table.
 
10) overwrite character Fields
To overwrite character fields with another character field, use the overlay statement as follows:
Syntax
Overlay <C1> with <c2> [Only <STR>].
This statement overwrites all the locations where the <C1> field contains letters in <STR> with the content of <c2>. <C2> remain unchanged. If only <STR> is omitted, all spaces in the <C1> field are overwritten.
If you want to replace at least one character in <C1>, set the SY-SUBRC to 0. For all other cases, set the SY-SUBRC to 4. If <C1> is longer than <c2>, only <c2>
.
 
11) obtain the string length
To determine the length of the string to the last character rather than the space, use the internal function strlen as follows:
Syntax
[Compute] <n> = strlen (<C> ).
Strlen processes the operand <C> as the character data type, regardless of its actual type. No conversion is performed.
 
12) Allocate the string part

The following variations of the move statement only use the C field:
Syntax
Move <C1> to <c2> Percentage <p> [right].
Copy the percentage of the left-aligned character field <C1> <p> (or right-aligned if specified by the right option) to <c2>.
<P> the value can be any number between 0 and 100. The length to be copied from <F1> is rounded to the next integer.
If a parameter in the statement is not of type C, the percentage parameter is ignored.
Data C1 (10) value 'abcdefghj ',
C2 (10 ).
Move C1 to C2 percentage 40.
Write C2.
Move C1 to C2 percentage 40 right.
Write/C2.
The output of this process is as follows:
ABCD
ABCD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.