3
votes

What are the drawbacks of the String type in abap? When to use it, when not?

An example : I have a text field that should save values ranging from 0 to 12 chars, better to use a string or a Char(12)?

Thanks!

5

5 Answers

7
votes

A string is stored as a dynamic array of characters while a char is statically allocated.

Some of the downsides of strings include:

  • Overhead - because they are dynamic the length must be stored in addition to the actual string.
  • The substring and offset operators don't work with strings.
  • Strings cannot be turned into translatable text elements.

So to answer your question, strings should only be used for fairly long values with a wide range of lengths where the additional overhead is negligible relative to the potential wasted space of a static char(x) variable.

2
votes

I think CHAR is the best because you are 100% sure that the field has to only hold 0-12 characters.

1
votes

string is the variable length Data type , while in char you have to define the length .. for type C(Text field (alphanumeric characters)) and String X or hexadecimal string have initial value (X'0 … 0') . to avoid initial value , and to use actual length C type is used

0
votes

String Variable : A String is a variable length data type which is used to store any length of data. Variable length fields are used because they save space. String, can store any number of characters. String will allocate the memory at runtime which is also called as dynamic memory allocation, will allocate the memory as per the size of the string. Strings cannot be declared using parameters as the memory of allocation is dynamic.

But in your case, you already know max-length of field(0 - 12 characters), So CHAR type is best for use in your case. A STRING type generally used to variable length data or a long values.

Read more

0
votes

Strings are good when:

  • The text length will be variable.
  • Spaces are part of the string (trailing spaces in CHAR fields are lost)
  • You pass them around a lot (when STRING variable metadata is less than char field size)
  • You need to get the STRING length often. It is more optimal than with CHAR fields.

CHAR fields are good:

  • If they are small, they are fast (less than around 32 chars on unicode systems)
  • CHAR field literals using (') quotes instead of (`) can be made into translatable texts.

Things to remember:

  • All variables have metadata, but strings also has some internal pointer to the string data, which could add up to 64 bytes to memory consumption. Something to keep in mind.
  • When assigning a literal text to a variable, try to match the literal type to the variable type. Use 'test' for CHAR and `test` for STRING. This is usually slightly faster.