Strings


Strings are amongst the most popular types in Spla. We can create them simply by enclosing characters in quotes. Spla treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example −

str var1 = 'Hello World!';
str var2 = "Spla Programming";

Accessing Values in Strings

Spla does not support a character type; these are treated as strings of length one, thus also considered a substring.

To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example −


str var1 = 'Hello World!';
str var2 = 'Spla Programming';

<< 'var1[0]: '+ var1[0];
<< 'var2[1:5]: '+ var2[1:5];

When the above code is executed, it produces the following result −

var1[0]:  H
var2[1:5]:  ytho

Updating Strings

You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. For example −


str var1 = 'Hello World!';
<< 'Updated String :- '+ var1[:6] + 'Spla';

When the above code is executed, it produces the following result −

Updated String :-  Hello Spla

Escape Characters

Following table is a list of escape or non-printable characters that can be represented with backslash notation.

An escape character gets interpreted; in a single quoted as well as double quoted strings.

Backslash notation Hexadecimal character Description
\n 0x0a Newline

String Special Operators

Assume string variable a holds 'Hello' and variable b holds 'Spla', then −

Operator Description Example
+ Concatenation - Adds values on either side of the operator a + b will give HelloSpla
* Repetition - Creates new strings, concatenating multiple copies of the same string a*2 will give -HelloHello
[] Slice - Gives the character from the given index a[1] will give e
[ : ]Range Slice - Gives the characters from the given range a[1:4] will give ell
inMembership - Returns true if a character exists in the given stringH in a will give 1
not in Membership - Returns true if a character does not exist in the given string M not in a will give 1
r/R Raw String - Suppresses actual meaning of Escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter "r," which precedes the quotation marks. The "r" can be lowercase (r) or uppercase (R) and must be placed immediately preceding the first quote mark. print r'\n' prints \n and print R'\n'prints \n
% Format - Performs String formatting See at next section

Built-in String Methods

Spla includes the following built-in methods to manipulate strings −

<<'Substring : '+strval[1:6]; <<'String length : '+strval.length<>; <<'String at 5 : '+strval[5]; <<'String Concat : '+strval.concat<' 1'>; <<'String endsWith examples1 : '+strval.endsWith<'examples1'>; <<'String endsWith examples : '+strval.endsWith<'examples'>; <<'String startsWith This1 : '+strval.startsWith<'This1'>; <<'String startsWith This : '+strval.startsWith<'This'>; <<'String indexOf This after 6 : '+strval.indexOf<'This',6>; <<'String indexOf This after 0 : '+strval.indexOf<'This'>; <<'String lastIndexOf i : '+strval.lastIndexOf<'i'>; <<'String replace This with Test : '+strval.replace<'This','Test'>; <<'String toLower : '+strval.toLower<>; <<'String toUpper : '+strval.toUpper<>; <<'String trim : '+strval.trim<>; str strval1='BabB'; !! <<'String Capitalilze : '+strval.capitalilze<>; !! <<'String Capitalilze : '+strval.ciynt<'this'>; !! <<'String Capitalilze : '+strval.encode<'UTF-8'>; !! <<'String Capitalilze : '+strval.decode<'UTF-8'>; !! <<'String Capitalilze : '+strval.expandtabs<>; !! <<'String isTitle : '+strval.isTitle<>; strval1='BabB3342.=+#(*'; <<'String '+strval1+' isAlpha : '+strval1.isAlpha<>; strval1='BabB3342.=+#(*'; <<'String '+strval1+' isDigit : '+strval1.isDigit<>; strval1='BabB3342'; <<'String '+strval1+' isAlNum : '+strval1.isAlNum<>; strval1='22.0'; <<'String '+strval1+' isDecimal : '+strval1.isDecimal<>; strval1='22.0'; <<'String '+strval1+' isNumeric : '+strval1.isNumeric<>; strval1='aaa'; <<'String aaa isLower : '+strval1.isLower<>; <<'String aaa isUpper : '+strval1.isUpper<>; strval1='AAA'; <<'String AAA isLower : '+strval1.isLower<>; <<'String AAA isUpper : '+strval1.isUpper<>; strval1='AAA'; <<'String AAA isSpace : '+strval1.isSpace<>; strval1='AAA bb'; <<'String AAA bb isSpace : '+strval1.isSpace<>; <<'String split : '+strval.split<' '>; str ss[]=['dd','ff','fg']; <<'String ss : '+ss; str sp[]=strval.split<' '>; <<'String split to array : '+sp; str sss[]; <<'String split to array : '+sss; sss=strval.split<' '>; <<'String split to array : '+sss; <<'\nString Functions end';
Sr.No. Methods & Description
1 strvar.length<>

Return length of string variable.

2 strvar.concat<' 1'>

Concat the given string with string variable and return.

3 strvar.endsWith<'examples1'>

Return true if ends with given string.

4 strvar.startsWith<'This1'>

Treutn true if starts with given string.

5 strvar.indexOf<'This',startPos>

Returns indexOf given string from startingPos.

6 strvar.indexOf<'This',startPos>

Returns indexOf given string from starting position.

7 strvar.lastIndexOf<'This'>

Return last index of given string.

8 strvar.replace<'Old','New'>

Replace new value with old value in the string variable.

9 strvar.toLower<>

Return lower case of the given string variable.

10 strvar.toUpper<>

Returns uper case of the given string variable.

11 strvar.trim<>

Trim string variable and return.