Post

String Literal

A string literal is used to create a string value. A string literal consists of a sequence of zero or more Unicode Code Points.

✍ Syntax

Double Quoted String Literal

" StringCharacters* "

Here StringCharacters is an Unicode Code Point except 0xA | 0xD | \ | "


✍ Syntax

String Escape

\t | \n | \r | \\ | \"

✍ Syntax

Numeric Escape

u{ Digit+ }

✍ Syntax

Double Quoted String Literal

A string literal can be written by having the Unicode code points wrapped using two double quotes ".

" StringCharacters* "

Here StringCharacters is an Unicode Code Point except 0xA | 0xD | \ | "

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import ballerina/io;

public function main() {

    string en = "Hello, World!";
    string zh = "你好世界!";
    string hi = "नमस्ते दुनिया!";
    string si = "හෙලෝ වර්ල්ඩ්!";
    string emoji = "👋🌏";

    io:println(en);
    io:println(zh);
    io:println(hi);
    io:println(si);
    io:println(emoji);
}
Hello, World!
你好世界!
नमस्ते दुनिया!
හෙලෝ වර්ල්ඩ්!
👋🌏
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    string en = "Hello, World!";
    string zh = "你好世界!";
    string hi = "नमस्ते दुनिया!";
    string si = "හෙලෝ වර්ල්ඩ්!";
    string emoji = "👋🌏";
}
// Highlights Only. Change View  to see full code.
string en = "Hello, World!";
string zh = "你好世界!";
string hi = "नमस्ते दुनिया!";
string si = "හෙලෝ වර්ල්ඩ්!";
string emoji = "👋🌏";

Double Quoted String Literal

String Literal With Escape Characters

There are certain code points, that are not allowed inside a string literal. You can use String Escape or Numeric Escape syntaxes to provide such code points.

String Escape syntax

\t | \n | \r | \\ | \"

This syntax starts with \, then followed by one of t, n, r, \, " letters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import ballerina/io;

public function main() {

    string tab = "tab :A\tB";
    string newline = "newline :A\nB";
    string carriageReturn = "carriageReturn :A\rB";
    string backSlash = "backSlash :A\\B";
    string doubleQuote = "doubleQuote :A\"B";

    io:println(tab);
    io:println(newline);
    io:println(carriageReturn);
    io:println(backSlash);
    io:println(doubleQuote);
}
tab :A	B
newline :A
B
carriageReturn :A
B
backSlash :A\B
doubleQuote :A"B
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    string tab = "tab :A\tB";
    string newline = "newline :A\nB";
    string carriageReturn = "carriageReturn :A\rB";
    string backSlash = "backSlash :A\\B";
    string doubleQuote = "doubleQuote :A\"B";
}
// Highlights Only. Change View  to see full code.
string tab = "tab :A\tB";
string newline = "newline :A\nB";
string carriageReturn = "carriageReturn :A\rB";
string backSlash = "backSlash :A\\B";
string doubleQuote = "doubleQuote :A\"B";

String Escape Example

Numeric Escape syntax

u{ Digit+ }

This syntax starts with u{, then hexadecimal numeral of the code point, followed by }. The hexadecimal numeral is an integer n where 0 ≤ n < 0xD800 or 0xDFFF < n ≤ 0x10FFFF.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import ballerina/io;

public function main() {

    string tab = "tab :A\u{9}B";
    string newline = "newline :A\u{A}B";
    string carriageReturn = "carriageReturn :A\u{D}B";
    string backSlash = "backSlash :A\u{5C}B";
    string doubleQuote = "doubleQuote :A\u{22}B";

    io:println(tab);
    io:println(newline);
    io:println(carriageReturn);
    io:println(backSlash);
    io:println(doubleQuote);
}
tab :A	B
newline :A
B
carriageReturn :A
B
backSlash :A\B
doubleQuote :A"B
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    string tab = "tab :A\u{9}B";
    string newline = "newline :A\u{A}B";
    string carriageReturn = "carriageReturn :A\u{D}B";
    string backSlash = "backSlash :A\u{5C}B";
    string doubleQuote = "doubleQuote :A\u{22}B";
}
// Highlights Only. Change View  to see full code.
string tab = "tab :A\u{9}B";
string newline = "newline :A\u{A}B";
string carriageReturn = "carriageReturn :A\u{D}B";
string backSlash = "backSlash :A\u{5C}B";
string doubleQuote = "doubleQuote :A\u{22}B";

Numeric Escape Example

String Literal

Navigation

Site Settings

Site Theme

Source Code Density