import ballerina/io;
import ballerina/lang.'string;

public function main() {

    string statement = "Lion in Town. Catch the Lion";

    string s1 = statement.toUpperAscii();
    io:println("ToUpper: ", s1);

    string s2 = statement.toLowerAscii();
    io:println("ToLower: ", s2);

    // Create a new `string`, which is a substring of the specified string.
    // You must provide the original `string`
    // and the starting and ending indexes of the substring.
    string s3 = statement.substring(0, 4);
    io:println("SubString: ", s3);

    // Retrieve the starting index of the first occurrence of the substring "on" within the `statement` string.
    int? index = statement.indexOf("on");
    if (index is int) {
        io:println("IndexOf: ", index);
    }

    // Retrieve the length of the `string`.
    int length = statement.length();
    io:println("Length: ", length);

    string hello = "Hello";
    string ballerina = "Ballerina!";

    // Concat multiple strings.
    string s4 = hello.concat(" ", ballerina);
    io:println("Concat: ", s4);

    // Join strings with a separator.
    string s5 = ",".'join(hello, ballerina);
    io:println("Join: ", s5);

    // Convert `hello` to a `byte` array.
    byte[] bArray = hello.toBytes();

    // Convert a `byte` array to a `string`.
    string|error s6 = 'string:fromBytes(bArray);
    if (s6 is string) {
        io:println("From bytes: ", s6);
    }

    // Remove leading and trailing white spaces.
    string toTrim = "  Ballerina Programming Language  ";
    string s7 = toTrim.trim();
    io:println("Trim: ", s7);

    // Check whether the given `string` ends with the suffix "Lion".
    boolean hasSuffix = statement.endsWith("Lion");
    io:println("HasSuffix: ", hasSuffix);

    // Check whether the given `string` starts with the prefix "Lion".
    boolean hasPrefix = statement.startsWith("Lion");
    io:println("HasPrefix: ", hasPrefix);

    // Format a `string` according to the given format arguments.
    string name = "Sam";
    int marks = 90;
    string[] subjects = ["English", "Science"];
    float average = 71.5;
    string s8 = io:sprintf("%s scored %d for %s and has an average of %.2f.",
     name, marks, subjects[0], average);
    io:println("Sprintf: ", s8);

    // Member access is allowed with strings to access individual characters
    // of a string. Member access panics if the integer index is out of range.
    string country = "Sri Lanka";
    string c = country[4];
    io:println("Member Access: ", c);
}

String

Ballerina contains a comprehensive set of functions to manipulate Strings.

import ballerina/io;
import ballerina/lang.'string;
public function main() {
    string statement = "Lion in Town. Catch the Lion";
    string s1 = statement.toUpperAscii();
    io:println("ToUpper: ", s1);
    string s2 = statement.toLowerAscii();
    io:println("ToLower: ", s2);
    string s3 = statement.substring(0, 4);
    io:println("SubString: ", s3);

Create a new string, which is a substring of the specified string. You must provide the original string and the starting and ending indexes of the substring.

    int? index = statement.indexOf("on");
    if (index is int) {
        io:println("IndexOf: ", index);
    }

Retrieve the starting index of the first occurrence of the substring “on” within the statement string.

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

Retrieve the length of the string.

    string hello = "Hello";
    string ballerina = "Ballerina!";
    string s4 = hello.concat(" ", ballerina);
    io:println("Concat: ", s4);

Concat multiple strings.

    string s5 = ",".'join(hello, ballerina);
    io:println("Join: ", s5);

Join strings with a separator.

    byte[] bArray = hello.toBytes();

Convert hello to a byte array.

    string|error s6 = 'string:fromBytes(bArray);
    if (s6 is string) {
        io:println("From bytes: ", s6);
    }

Convert a byte array to a string.

    string toTrim = "  Ballerina Programming Language  ";
    string s7 = toTrim.trim();
    io:println("Trim: ", s7);

Remove leading and trailing white spaces.

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

Check whether the given string ends with the suffix “Lion”.

    boolean hasPrefix = statement.startsWith("Lion");
    io:println("HasPrefix: ", hasPrefix);

Check whether the given string starts with the prefix “Lion”.

    string name = "Sam";
    int marks = 90;
    string[] subjects = ["English", "Science"];
    float average = 71.5;
    string s8 = io:sprintf("%s scored %d for %s and has an average of %.2f.",
     name, marks, subjects[0], average);
    io:println("Sprintf: ", s8);

Format a string according to the given format arguments.

    string country = "Sri Lanka";
    string c = country[4];
    io:println("Member Access: ", c);
}

Member access is allowed with strings to access individual characters of a string. Member access panics if the integer index is out of range.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run strings.bal
ToUpper: LION IN TOWN. CATCH THE LION
ToLower: lion in town. catch the lion
SubString: Lion
IndexOf: 2
Length: 28
Concat: Hello Ballerina!
Join: Hello,Ballerina!
From bytes: Hello
Trim: Ballerina Programming Language
HasSuffix: true
HasPrefix: true
Sprintf: Sam scored 90 for English and has an average of 71.50.
Member Access: L