Module : stringutils
Module overview
This module provides utility functions to manipulate built-in string
data type.
Samples
Compare two strings ignoring the case
import ballerina/io;
import ballerina/stringutils;
public function main() {
string str1 = "Comparing String";
string str2 = "cOmpaRinG sTrinG";
io:print("String '" + str1 + "' and '" + str2 + "' are");
if (stringutils:equalsIgnoreCase(str1, str2)) {
io:println(" equal");
} else {
io:println(" not equal");
}
}
Replace a substring in a string, using a regex
import ballerina/io;
import ballerina/stringutils;
public function main() {
string str = "OriginalPPPString";
string regex = "P+";
string newString = stringutils:replaceAll(str, regex, " ");
// newString will be 'Original String'
io:println(newString);
}
Split a string
import ballerina/io;
import ballerina/stringutils;
public function main() {
string stringsArray = "Sam,Ru,Supun";
string[] result = stringutils:split(stringsArray, ",");
// result will be {"Sam", "Ru", "Supun"}
foreach var str in result {
io:println(str);
}
}
contains | Checks whether the given string contains a particular substring. |
equalsIgnoreCase | Checks if two strings are equal, ignoring the case of the strings. |
hashCode | Returns a hash code for a given string. |
lastIndexOf | Returns the last index of the provided substring within a string. |
matches | Checks whether the given string matches with the provided regex. |
replace | Replaces each substring of the provided string, that matches the provided substring, with the specified replacement string. |
replaceAll | Replaces each substring which matches the given regular expression, from the given original string value, with the specified replacement string. |
replaceFirst | Replaces the first substring that matches the given sequence from the provided string, with the specified literal replacement sequence. |
split | Splits a string using the given delimiter. |
toBoolean | Returns a boolean value of a given string. |