Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Translate

Former Member
0 Kudos
1,121

Can you tell me what 'translate' does ?

I know it converts uppercase to lowercase and viceversa

But particularly if it means "translate p using q'. What is it doing here?

An example would be helpful.

Thank you very much.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Kudos
729

hi,

Translate abc using pattern.

If you specify USING, the characters in text are converted according to the rule specified in pattern. pattern must be a character-type data object whose contents are interpreted as a sequence of character pairs. text is searched for the first character of each pair, starting with the first pair, and each location found is replaced with the second character of the pair. The search is case-sensitive. If pattern contains a character multiple times as the first character of a pair, only the first pair is taken into account. A character in text that has already been replaced cannot be replaced again in the same TRANSLATE statement. Therefore, if the second character of a pair in pattern appears as the first character of a subsequent pair, the second pair affects only the original characters in text.

Trailing blanks in data objects text and pattern are taken into account for data objects. If pattern contains an uneven number of characters, the last character is ignored. If pattern is a blank string, no replacements take place.

Converts the characters "A" to "B", "a" to "b", and vice versa. text contains "Abracadabra" after the conversion.

DATA text TYPE string.

text = `Barbcbdbarb`.

TRANSLATE text USING 'ABBAabba'.

Regards,

Richa..

6 REPLIES 6
Read only

Former Member
0 Kudos
729

Hi nuren,

it replaces every occurence of p with q in the string specified.

also check this:

REPLACE by Pattern

Replaces strings in fields with other strings using a pattern.

Syntax

REPLACE [ FIRST OCCURENCE OF | ALL OCCURENCES OF ] <old>

IN [ SECTION OFFSET <off> LENGTH <len> OF ] <text> WITH <new>

[IGNORING CASE|RESPECTING CASE]

[IN BYTE MODE|IN CHARACTER MODE]

[REPLACEMENT COUNT <c>]

[REPLACEMENT OFFSET <r>]

[REPLACEMENT LENGTH <l>].

In the string <text>, the search pattern <old> is replaced by the content of <new>. By default, the first occurrence of <old> is replaced. ALL OCCURENCES specifies that all occurrences be replaced. In the fields <old> and <new>, trailing spaces in C fields are ignored, but included in <text>. The SECTION OFFSET <off> LENGTH <len> OF addition tells the system to search and replace only from the <off> position in the length <len>. IGNORING CASE or RESPECTING CASE (default) specifies whether the search is to be case-sensitive. In Unicode programs, you must specify whether the statement is a character or byte operation, using the IN BYTE MODE or IN CHARACTER MODE (default) additions. The REPLACEMENT additions write the number of replacements, the offset of the last replacement, and the length of the last replaced string <new> to the fields <c>, <r>, and <l>.

reward if it helps.

hope this helps,

keerthi.

Read only

Former Member
0 Kudos
729

TRANSLATE c USING c1.

Effect

Translates the contents of c according to therule in field c1.

When a character from c corresponds to a character from c1, it is replaced by the next character that occurs in c1. Ifthe character appears more than once in c1, its first occurrenceis used in the replacement. If a character from c does not occurin c1, it remains unchanged

vengal

Read only

Former Member
0 Kudos
729

Hi Nuren,

Check this.

The TRANSLATE statement converts characters into upper or lower case, or uses substitution rules to convert all occurrences of one character to another character.

Converting to Upper or Lower Case

TRANSLATE <c> TO UPPER CASE.

TRANSLATE <c> TO LOWER CASE.

These statements convert all lower case letters in the field <c> to upper case or vice versa.

Substituting Characters

TRANSLATE <c> USING <r>.

This statement replaces all characters in field <c> according to the substitution rule stored in field <r> . <r> contains pairs of letters, where the first letter of each pair is replaced by the second letter. <r> can be a variable.

For more variants of the TRANSLATE statement with more complex substitution rules, see the keyword documentation in the ABAP Editor.

DATA: T(10) VALUE 'AbCdEfGhIj',

STRING LIKE T,

RULE(20) VALUE 'AxbXCydYEzfZ'.

STRING = T.

WRITE STRING.

TRANSLATE STRING TO UPPER CASE.

WRITE / STRING.

STRING = T.

TRANSLATE STRING TO LOWER CASE.

WRITE / STRING.

STRING = T.

TRANSLATE STRING USING RULE.

WRITE / STRING.

Output:

AbCdEfGhIj

ABCDEFGHIJ

abcdefghij

xXyYzZGhIj

TRANSLATE ... TO UPPER/LOWER CASE

TRANSLATE ... USING

The arguments of these instructions must be single fields of type C, N, D, T or STRING or structures of character-type only. There is a syntax or runtime error if arguments of a different type are passed. A subset of this function is provided with the addition IN BYTE MODE for processing byte strings – that is, operands of type X or XSTRING. A statement such as CONCATENATE a x b INTO c is thus no longer possible when a,b, and c are all character-type, but x is of type X.

TRANSLATE ... CODEPAGE ...

TRANSLATE ... NUMBER FORMAT ...

The above statements are not allowed in Unicode programs. Instead, you can use the new conversion classes, which are described in more detail in Converting Data.

These classes are used to convert ABAP data from the system format to external formats and vice versa. During this conversion process, character-type data may be converted to another character set, while numeric-type data may be converted to another byte order (or endian format). You must use a container of type X or XSTRING for data in an external format.

Character sets and endian formats are also converted by the file interface (OPEN DATASET with new additions), RFCs, and the function modules GUI_DOWNLOAD and GUI_UPLOAD. The classes described below are available for those special cases where the possibilities offered by conversion are insufficient. Since these classes work with containers of types X and XSTRING, these containers can be copied unconverted by the file interface (OPEN DATASET with new additions), RFCs, and the function modules GUI_DOWNLOAD and GUI_UPLOAD. These classes replace the following two statements:

TRANSLATE c ...FROM CODE PAGE g1 ... TO CODE PAGE g2

TRANSLATE f ...FROM NUMBER FORMAT n1 ... TO NUMBER FORMAT n2

For a detailed description, see the class documentation in the Class Builder. The following classes are available:

CL_ABAP_CONV_IN_CE

Reads data from a container and converts it to the system format. You can also fill structures with data.

CL_ABAP_CONV_OUT_CE

Converts data from the system format to an external format and writes it to a container.

CL_ABAP_CONV_X2X_CE

Converts data from one external format to another.

Reward if it helps,

Regards,

laxmi.

Message was edited by: Laxmi

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
729

Translate, in this case, will replace all occurances of a "-" with a space.

 translate p_number using '- '.

Another example, say that you want do translate all commas, to periods. You could do this.

 translate p_number using '-.'.

Regards,

Rich Heilman

Read only

Former Member
0 Kudos
730

hi,

Translate abc using pattern.

If you specify USING, the characters in text are converted according to the rule specified in pattern. pattern must be a character-type data object whose contents are interpreted as a sequence of character pairs. text is searched for the first character of each pair, starting with the first pair, and each location found is replaced with the second character of the pair. The search is case-sensitive. If pattern contains a character multiple times as the first character of a pair, only the first pair is taken into account. A character in text that has already been replaced cannot be replaced again in the same TRANSLATE statement. Therefore, if the second character of a pair in pattern appears as the first character of a subsequent pair, the second pair affects only the original characters in text.

Trailing blanks in data objects text and pattern are taken into account for data objects. If pattern contains an uneven number of characters, the last character is ignored. If pattern is a blank string, no replacements take place.

Converts the characters "A" to "B", "a" to "b", and vice versa. text contains "Abracadabra" after the conversion.

DATA text TYPE string.

text = `Barbcbdbarb`.

TRANSLATE text USING 'ABBAabba'.

Regards,

Richa..

Read only

Former Member
0 Kudos
729

Hi,

trasslate can be used to replace all occurences

of particular value

DATA: LETTERS(10) VALUE 'abcX',

CHANGE(6) VALUE 'aXBY'.

TRANSLATE LETTERS USING CHANGE.

the field LETTERS now contains 'XbcX'.

Regards

Anmole