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";
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
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
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 |
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 |
| in | Membership - Returns true if a character exists in the given string | H 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 |
Spla includes the following built-in methods to manipulate strings −
| 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. |