Application Development 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: 

TRANSLATE

Former Member
0 Kudos

Hi all,

Can anybody please explain me what is the exact use of TRANSLATE as I have seen an example for TRANSLATE in abap help it is as follows.

DATA text TYPE string.

text = `Barbcbdbarb`.

TRANSLATE text USING 'ABBAabba'.

the output is : Abracadabra

I have gone through the documentation provided there, But Iam not clear with that documentation. Please help me

Thanks in advance

9 REPLIES 9

Former Member
0 Kudos

Hi,

Translate converts letters into Upper case to Lower case or vice versa.

Reward if useful!

0 Kudos

Hi Dixit,

I know that but what is use of the below mentioned statement when we add USING to it succeded by the text.

TRANSLATE text USING 'ABBAabba'.

0 Kudos

Hi Ranjith..

data :v_str(10) value 'ABCBBC' .

TRANSLATE v_str USING 'AXBY'.

write:/ v_str.

Translate in this case Will be used for Character Replacement of all occurrences in a String .

So It will Replace all the characters 'A' with 'X'

and all the characters 'B' with 'Y'.

That means it will divide the String 'AXBY' into Pairs of Characters and In each Pair the First Character Occurrences will be Replaced with The Second Character...

So the Result will be : XYCYYC.

<b>REWARD IF HELPFUL.</b>

Former Member

Hi Ranjith,

TRANSLATE is mainly used to convert the text in to upper case, lower case or using any specific rule.

DATA: T(10) VALUE 'AbCdEfGhIj',

STRING LIKE T,

RULE(20) VALUE 'AxbXCydYEzfZ'.

STRING = T.

TRANSLATE STRING USING RULE.

WRITE / STRING.

Output will be <b>xXyYzZGhIj</b>

This means the rule has been defined like

A which is in T(10) should be converted to Lower case indicated by "x" 2nd letter in the RULE while the first letter in RULE denotes the letter (from T) itself.

In other terms the mapping is like bwloe:

T(10) VALUE 'AbCdEfGhIj',

RULE(20) VALUE 'AxbXCydYEzfZ'.

Here

A in T is mapped with x in RULE.

b in T is mapped with X in RULE.

C in T is mapped with y in RULE.

and goes on....

<b>Reward points for helpful answers</b>.

Best Regards,

Ram.

0 Kudos

Hi Ram,

Thank you very much. It was a very good example. Thanks a Lot

Former Member
0 Kudos

Hi Ranjeet,

Check this link

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/translat.htm

TRANSLATE text USING 'ABBAabba'

This translates text using the character set specified in the USING clause.

Here it replaces A with B, B with A, a with b and b with a in the text.

In the USING clause, the second character is replaced with first character in the string and so on.

Thanks,

Vinay

Former Member
0 Kudos

TRANSLATE c USING r.

Effect

Replaces all the characters in the field c according to the rule in field r. c1 containscharacter pairs. The second character in each pair replaces the first. If r does not contain a character in c, this character remains unchanged. r can also be a variable.

Example

DATA: letters(20) TYPE C VALUE 'abcabcabcXab',

change(15) TYPE C VALUE 'aXbaYBabZacZ'.

TRANSLATE letters USING change.

letters now contains 'XaZXaZXaZXXaZ'.

Former Member
0 Kudos

HI

<u><b>TRANSLATE</b></u>

TRANSLATE text {TO {UPPER|LOWER} CASE}

| {USING pattern}.

This statement converts the case or single characters of the character-type data object text. The statement CASE can be used for the conversion to upper/lower case; USING can be used for the conversion according to a pattern. The variable text must be character-type.

There are two obsolete variants of this statement:

Addition 1

... TO {UPPER|LOWER} CASE

If you specify UPPER, all lower-case letters of the data object text are converted to upper case. If you specify LOWER, all upper-case letters are converted to lower case.

The conversion of the upper/lower case depends on the text environment. Problems may occur if the language of the text environment differs from the language in which the data to be processed is entered. Data loss will occur if a conversion between the source and target language has not been defined. To avoid this type of inconsistency, you must appropriately set the text environment using the SET LOCALE statement prior to the conversion.

Example

After the conversion, the variable text contains "CAREFUL WITH THAT AXE, EUGENE".

DATA text TYPE string.

text = `Careful with that Axe, Eugene`.

TRANSLATE text TO UPPER CASE.

Addition 2

... 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.

Example

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'.

REWARD IF USEFULL

Former Member
0 Kudos

Hi Ranjithm

TRANSLATE text USING 'ABBAabba'.

In the above statement it takes 2 letters at a time

say the first 2 letters are , AB , here A will be converted to B in text

next 2 letter are BA , so B will be converted to A in text

next 2 letters are ab , a will be converted to b

and so on..