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: 

String conversion

Former Member
0 Kudos

Hi folks,

I have one string in my program. The value in that is "TEN THOUSAND ONLY". Now I want to display this string as "Ten Thousand Only". The first character after the space must be caps and others must be in small letters". Can anbody please provide some FM for this issue.

Points will be awarded.

Regards

Ravi

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

Look at this. You can use Translate with USE PATTERN option. See this Docu.

... USING pattern

Effect

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'

May be you can try it like this.

Translate text using 'Ten Thousand Only'

Regards

Haritha.

6 REPLIES 6

Former Member
0 Kudos

Hi

Look at this. You can use Translate with USE PATTERN option. See this Docu.

... USING pattern

Effect

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'

May be you can try it like this.

Translate text using 'Ten Thousand Only'

Regards

Haritha.

Former Member
0 Kudos

Hi,

Use FM STRING_UPPER_LOWER_CASE.

Regards,

Himanshu

Former Member
0 Kudos

hi

sample code for ur requirement is as below:

1. TRANSLATE c TO UPPER CASE.

2. TRANSLATE c TO LOWER CASE.

3. TRANSLATE c USING c1.

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

5. TRANSLATE c FROM NUMBER FORMAT n1 TO NUMBER FORMAT n2.

Variant 1

TRANSLATE c TO UPPER CASE.

Variant 2

TRANSLATE c TO LOWER CASE.

Effect

In the field c , converts all lower case letters to upper case or all upper case letters to lower case, as specified.

Example

DATA LETTERS(3).

MOVE 'abc' TO LETTERS.

TRANSLATE LETTERS TO UPPER CASE.

The field LETTERS now contains 'ABC' .

Variant 3

TRANSLATE c USING c1.

Effect

Replaces the letters in the field c according to the contents of c1 .

If a character in c also exists in c1, it is replaced by the letter that f o l l o w s it in c1. If a character from c is not found in c1, it remains unchanged.

Example

DATA: LETTERS(10) VALUE 'abcX',

CHANGE(6) VALUE 'aXBY'.

TRANSLATE LETTERS USING CHANGE.

The field LETTERS now contains 'XbcX' .

Variant 4

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

TRANSLATE F TO CODE PAGE G2.

TRANSLATE F FROM CODE PAGE G1.

Effect

Performs a character code conversion in the field F . To achieve this, the SAP character code is determined from the conversion table G1 and a new character code derived from G2 . You can use the Transaction SPAD to maintain the conversion tables TCP00 - TCP02 .

Example

DATA F(72).

TRANSLATE F FROM CODE PAGE '1110' TO CODE PAGE '0100'.

translates the contents of F from the HP character set to EBCDIC ( IBM 274).

Note

Type I , P , F and X fields remain unchanged. Field strings and work areas of internal tables are converted to the correct type for each individual field. At present, table work areas (as defined in TABLES ... ) are not treated according to type, but are converted as a whole. If necessary, declare a field string with INCLUDE STRUCTURE and then perform a conversion.

Variant 5

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

TRANSLATE F TO NUMBER FORMAT N1.

TRANSLATE F FROM NUMBER FORMAT N1.

Effect

Performs a number format conversion in the field F . The number formats supported at present are '0000' ( HP , SINIX , IBM ) and '0101' ( DEC alpha OSF ). Any attempt to enter formats other than these results in a runtime error. If you omit FROM NUMBER FORMAT or TO NUMBER FORMAT , the system number format is used for the omitted part.

Example

DATA: F TYPE F,

HEX (2) TYPE X,

NFORM LIKE TCP00-CPCODEPAGE.

...

  • In /ARCHIV was stored by another platform from HEX and F.

  • HEX contains the valid number format and can be read on all

  • platforms.

READ DATASET '/ARCHIV' INTO HEX.

READ DATASET '/ARCHIV INTO F.

NFORM = HEX. "Conversion of machine-independent HEX to NUMC(4)

TRANSLATE F FROM NUMBER FORMAT NFORM.

Effect

Converts the contents of F from the format NFORM of a platform to the system format.

Note

Type I and F fields are converted. Field strings and work areas of internal tables are converted to the correct type for each individual field. Table work areas (as defined with TABLES ... ) are treated as type C at present and are not converted. If necessary, declare a field string with INCLUDE STRUCTURE and then perform a conversion.

In the interests of storing additional information for archiving purposes, you can use the function module SYSTEM_FORMAT to display the system code page and system number format.

Note

Performance

Converting lower case letters to upper case letters or upper case letters to lower case letters in a 10-byte long character field takes approx. 7 msn (standardized microseconds).

Replacing two letters in a 10-byte long field with the variant ... c USING c1 ... takes approx. 9 msn.

regards

ravish

<b>plz reward if helpful</b>

sushant_singh
Participant
0 Kudos

Hi ,

First of all convert the string into lower case then

you can use statement TRANSLATE for this conversion.

TRANSLATE STR USING RULE. ( RULE can be used for the desired output)

Former Member
0 Kudos

hi

you can use this logic..



REPORT  ZCONVERTCASE.

PARAMETERS: a(50).

DATA: a1(50), a2(50), a3(50), a4(50), a5(50), a6(50), b(100).

SPLIT a AT space INTO a1 a2 a3 a4 a5.

PERFORM convert_lower_case CHANGING a1.
PERFORM convert_lower_case CHANGING a2.
PERFORM convert_lower_case CHANGING a3.
PERFORM convert_lower_case CHANGING a4.
PERFORM convert_lower_case CHANGING a5.


CONCATENATE a1 a2 a3 a4 a5 INTO b SEPARATED BY space.

write b.

*&---------------------------------------------------------------------*
*&      Form  CONVERT_LOWER_CASE
*&---------------------------------------------------------------------*
FORM convert_lower_case  CHANGING    p_a1.

  CALL FUNCTION 'STRING_UPPER_LOWER_CASE'
    EXPORTING
      delimiter = 'X'
      string1   = p_a1
    IMPORTING
      string    = p_a1
    EXCEPTIONS
      not_valid = 1
      too_long  = 2
      too_small = 3
      OTHERS    = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    " CONVERT_LOWER_CASE

Former Member
0 Kudos

Hi Ravi,

split 'TEN THOUSAND ONLY' and for each word call FM STRING_UPPER_LOWER_CASE

Regards,

Tanmay