Skip to Content
avatar image
Former Member

TRANSLATE

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • avatar image
    Former Member
    Aug 30, 2007 at 10:02 AM

    Hi,

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

    Reward if useful!

    Add comment
    10|10000 characters needed characters exceeded

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

  • avatar image
    Former Member
    Aug 30, 2007 at 10:07 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 30, 2007 at 10:09 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 30, 2007 at 10:10 AM

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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 30, 2007 at 10:16 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 30, 2007 at 11:37 AM

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

    Add comment
    10|10000 characters needed characters exceeded