Tutorial - String Functions

This tutorial is aiming to teach you the function of the String Library used by Sprite Basic Compiler. Those functions are available for any variable of type string, at any time during program life-cycle.

string.contains(string tosearch) : boolean

returns true if tosearch is included in orginal string, false otherwise

boolean b="Coca Cola".contains("Co") // true

string.indexOf(string tosearch) : number

returns index of tosearch within string, starting at position zero. Returns -1 if tosearch not found.

number index="Hello World".indexOf("World") //6

string.lastIndexOf(string tosearch) : number

returns last position of tosearch within string, starting at position zero. Returns -1 if tosearch not found.

number index="Coca Cola".lastIndexOf("Co") // 5

string.replace(string str1, string str2) : string

Replace the first occurence of str1 by str2 in a given string

"Coca Cola".replace("Max") // Maxca Cola

string.replaceAll(string str1, string str2) : string

Replace all occurences of str1 by str2 in a given string

"Coca Cola".replaceAll("Max") // Maxca Maxla

string.substring(number index1, [number index2]) : string

Return the substring from index1 inclusive up to index2 non inclusive.
Indexes start at position zero.If only index1 is provided, returns substring from index1 inclusive up to the end of original string.
This method doesn"t change the original string.

string str="Hello World".substring(6) // World