ABAP Predefined Case functions

 Today's topic is case handling,

Abap has support for handling cases of string using both keywords and built-in methods.

I wanted to talk about the built-in methods that you can use to modify the case of a string and how they are better than their keyword counterparts.

1. To_upper:

Converts the input string/literal to uppercase and returns the uppercased string which can be used in operand positions.

Example:

REPORT sy-repid.
PARAMETERS:
p_inp TYPE char11 DEFAULT 'Hello World'.
START-OF-SELECTION.
*old way..
DATA(out) = p_inp.
TRANSLATE out TO UPPER CASE.
write:/ 'Uppercase: ', out. " Output -> HELLO WORLD
"New way
write:/ 'Uppercase: ', to_upper( p_inp ). " Output -> HELLO WORLD
view raw to_upper.abap hosted with ❤ by GitHub

2. To_lower:

Converts the input string/literal to lowercase and returns the uppercased string which can be used in operand positions. 

Example:

REPORT sy-repid.
PARAMETERS:
p_inp TYPE char11 DEFAULT 'Hello World'.
START-OF-SELECTION.
*old way..
DATA(out) = p_inp.
TRANSLATE out TO LOWER CASE.
write:/ 'Lowercase: ', out. "Output-> hello world
"New Way
write:/ 'Lowercase: ', to_lower( p_inp ). "Output-> hello world
view raw to_lower.abap hosted with ❤ by GitHub

3. To_mixed:

Converts the input string/literal to mixed case and returns the uppercased string which can be used in operand positions. 

There is no equivalent keyword for this function and the function can be really helpful in manipulating the string cases.

Example:

Of course, it's just a demonstration, you can play around with a more complex scenario of mixed case function. 

REPORT sy-repid.
START-OF-SELECTION.
"Default behaviour
write:/ 'Mixedcase: ', to_mixed( 'HELLO WORld'). "Output-> Hello world
"first letter Upper case after separator.
write:/ 'Mixedcase with searator: ', to_mixed( val = 'HELLO WORld'
sep = ` ` ). "Output-> HelloWorld
"Capitalise specific letter
write:/ 'Mixedcase: ', to_mixed( val = 'HELLO WORld'
case = 'hW'
sep = ` `). "Output-> helloWorld
view raw to_mixed.abap hosted with ❤ by GitHub

As always, hope you like the post and use it in your day-to-day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP