import ballerina/io;function main (string[] args) {    string statement = "Lion in Town. Catch the Lion ";    string s1 = statement.toUpperCase();
    io:println("ToUpper: " + s1);    string s2 = statement.toLowerCase();
    io:println("ToLower: " + s2);
    boolean isEqual = statement.equalsIgnoreCase("lion in town. catch the lion ");
    io:println("EqualsIgnoreCase: " + isEqual);
    string s3 = statement.subString(0, 4);
    io:println("SubString: " + s3);    boolean contains = statement.contains("Lion");
    io:println("Contains: " + contains);
    int index = statement.indexOf("on");
    io:println("IndexOf: " + index);
    int lastIndex = statement.lastIndexOf("on");
    io:println("LastIndexOf: " + lastIndex);
    float value = 5.8;
    string s4 = <string>value;
    io:println("ValueOf: " + s4);
    string s5 = statement.replaceFirst("Lion", "Tiger");
    io:println("ReplaceFirst: " + s5);
    string s6 = statement.replace("Lion", "Tiger");
    io:println("Replace: " + s6);
    string s7 = statement.replaceAll("[o]+", "0");
    io:println("ReplaceAll: " + s7);
    int length = statement.length();
    io:println("Length: " + length);
    string s8 = statement.trim();
    io:println("Trim: " + s8);
    boolean hasSuffix = statement.hasSuffix("Lion ");
    io:println("HasSuffix: " + hasSuffix);
    boolean hasPrefix = statement.hasPrefix("Lion");
    io:println("HasPreffix: " + hasPrefix);
    string s9 = statement.unescape();
    io:println("Unescape: " + s9);
    string[] array = statement.split(" ");
    io:println("Split: " + array[0]);
    io:println("Split: " + array[1]);
    io:println("Split: " + array[2]);
    blob blobValue = statement.toBlob("UTF-8");
    string s10 = blobValue.toString("UTF-8");
    io:println("Blob: " + s10);
    string s11 = io:sprintf("%s %f", [array[0], value]);
    io:println("Sprintf: " + s11);
}

Strings

Ballerina contains a comprehensive set of functions to manipulate strings.

import ballerina/io;
function main (string[] args) {
    string statement = "Lion in Town. Catch the Lion ";
    string s1 = statement.toUpperCase();
    io:println("ToUpper: " + s1);
    string s2 = statement.toLowerCase();
    io:println("ToLower: " + s2);
    boolean isEqual = statement.equalsIgnoreCase("lion in town. catch the lion ");
    io:println("EqualsIgnoreCase: " + isEqual);

Compares two strings, ignoring the case. Returns true if the strings are equal and false otherwise.

    string s3 = statement.subString(0, 4);
    io:println("SubString: " + s3);

Returns a new string that is a substring of the specified string. You should give the original string and the starting and ending indexes of the substring.

    boolean contains = statement.contains("Lion");
    io:println("Contains: " + contains);
    int index = statement.indexOf("on");
    io:println("IndexOf: " + index);

Returns the first index of the first occurrence of the substring within the specified string.

    int lastIndex = statement.lastIndexOf("on");
    io:println("LastIndexOf: " + lastIndex);

Returns the first index of the last occurrence of the substring within the specified string.

    float value = 5.8;
    string s4 = <string>value;
    io:println("ValueOf: " + s4);

Converts a value of type float to a string.

    string s5 = statement.replaceFirst("Lion", "Tiger");
    io:println("ReplaceFirst: " + s5);

Replaces the first instance of the replacePattern with the replaceWith string.

    string s6 = statement.replace("Lion", "Tiger");
    io:println("Replace: " + s6);

Replaces the replacePattern string with the replacement string.

    string s7 = statement.replaceAll("[o]+", "0");
    io:println("ReplaceAll: " + s7);

Replaces each substring of the replacePattern that matches the given regular expression with the replacement string.

    int length = statement.length();
    io:println("Length: " + length);

Returns the length of the string.

    string s8 = statement.trim();
    io:println("Trim: " + s8);

Removes any leading and trailing white spaces.

    boolean hasSuffix = statement.hasSuffix("Lion ");
    io:println("HasSuffix: " + hasSuffix);

Tests if this string ends with the specified suffix.

    boolean hasPrefix = statement.hasPrefix("Lion");
    io:println("HasPreffix: " + hasPrefix);

Tests if this string starts with the specified prefix.

    string s9 = statement.unescape();
    io:println("Unescape: " + s9);

Returns an unescaped string by omitting the escape characters of the original string.

    string[] array = statement.split(" ");
    io:println("Split: " + array[0]);
    io:println("Split: " + array[1]);
    io:println("Split: " + array[2]);

Splits the string with the given regular expression to produce a string array.

    blob blobValue = statement.toBlob("UTF-8");

Converts a string to a BLOB.

    string s10 = blobValue.toString("UTF-8");
    io:println("Blob: " + s10);

Converts a value of type BLOB to a string.

    string s11 = io:sprintf("%s %f", [array[0], value]);
    io:println("Sprintf: " + s11);
}

Formats a string according to given format arguments.

$ ballerina run strings.bal
ToUpper: LION IN TOWN. CATCH THE LION
ToLower: lion in town. catch the lion
EqualsIgnoreCase: true
SubString: Lion
Contains: true
IndexOf: 2
LastIndexOf: 26
ValueOf: 5.8
ReplaceFirst: Tiger in Town. Catch the Lion
Replace: Tiger in Town. Catch the Tiger
ReplaceAll: Li0n in T0wn. Catch the Li0n
Length: 29
Trim: Lion in Town. Catch the Lion
HasSuffix: true
HasPreffix: true
Unescape: Lion in Town. Catch the Lion
Split: Lion
Split: in
Split: Town.
Blob: Lion in Town. Catch the Lion
Sprintf: Lion 5.8