What, How, When to Use a String
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. Example:
Raj is 3 characters long, Vemuri Rajgopal is 14 character long. With fixed-length fields, you need to define each field to be long enough to hold the longest name. This would waste space for records that had short names. In variable length fields it stores the exact length of the string. • 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. • RFC(Remote Function Call) type for string is RFC_String. RFC is a interface for communication between a SAP system and other SAP or third party compatible system. • The XSD type for string is xsd:string.
• To convert a string to xstring we use the function module scms_string_to_xstring Syntax:
DATA [()] TYPE [DECIMALS ]… [VALUE ].
DATA : V_NAME TYPE STRING.
V_NAME = ‘RAJ’.
Memory is allocated only for RAJ, which always depends on the number of characters stored currently. Advantages of using a String:
• Efficient management of string storage space.
• String values are stored separately from variables in memory. Operations Of Strings:
• Concatenate : To club more than one sub string into one main string. Syntax :
Concatenate ….. into separated by a .
Concatenate ‘code’ ‘excellence’ into s_name separated by ‘,’. Output : code,excellence
• Condense : Replaces sequence of spaces into one space. Syntax :
Condense ‘ ’.
1. Condense ‘code excellence’. - All the blanks in the above example are converted into a single blank. Output : code excellence
2. Condense ‘code excellence’ no-gaps. - There will not be any gap between the two strings. Output : codeexcellence
• Translate : Used to translate the string into upper/lower case. Syntax :
TRANSLATE TO UPPERCASE/LOWERCASE.
TRANSLATE ‘CODE EXCELLENCE’ TO LOWERCASE.
Output : code excellence
• Replace : Replaces the string, With replace we can only replace the first occurrences of the string. Syntax :
REPLACE WITH INTO .
- Replace only the first occurrence (but not all).
REPLACE ‘I’ WITH ‘E’ INTO ‘INTELLIGENT’.
Output : ENTELLIGENT.
• Split : To split the main string into substrings by the given separators. Syntax :
SPLIT AT INTO … .
SPLIT ‘C99,CODE EXCELLENCE,USA’ AT ‘,’ INTO S_ID S_NAME S_COUNTRY. Note : S_ID,S_NAME,S_COUNTRY should be declared as strings.
S_ID - C99 S_NAME - CODE EXCELLENCE S_COUNTRY - USA
• Shift : Shifts the string by one place, by default it will shift to left side. String can be shifted to left, right, circular. Syntax :
1. SHIFT BY PLACES.
2. SHIFT LEFT DELETING LEADING .
3. SHIFT RIGHT DELETING TRAILING .
1. SHIFT ‘ARISE’. -- MAX (Since default LEFT). 2. SHIFT ‘ARISE’ CIRCULAR -- RISEA.
3. SHIFT ‘ARISE’ RIGHT BY 2 PLACES -- ‘ ARISE’ (First two characters are places. • Search : Searches for the required string in the main string. o ‘str’ – a character string (trailing spaces are ignored). o ‘.str.’ – any string between the periods. o ‘*str’ - a word ending with str.
o Str*’ – a word beginning with str.
Comparing Strings :
• CO(Contains Only) :
IF CO .
Is true if contains only characters from . The comparison is case sensitive. Trailing blanks are included. If the comparison of the string Is...