Skip to main content

String Class

String is Alia's built-in string class, used to represent and process text data.

Creating Strings​

Literals​

const str1 = 'Hello World'
const str2 = "Hello World"

Constructor​

const str3 = new String 'Hello World'
const str4 = String 'Hello World'

Instance Methods​

length​

Returns the length of the string.

const str = 'Hello World'
print str.length ~ outputs 11

split​

Splits the string into an array. Splitting supports a limit to restrict the number of items.

const str = 'a,b,c,d'
const arr = str.split ','
print arr ~ outputs ['a', 'b', 'c', 'd']
const arr2 = str.split ',' limit 2
print arr2 ~ outputs ['a', 'b']

replace​

Replaces all matches in the string.

const str = 'Hello World'
const newStr = str.replace 'World' with 'Alia'
print newStr ~ outputs 'Hello Alia'

slice​

Extracts a portion of the string.

const str = 'Hello World'
print str.slice 0 to 5 ~ outputs 'Hello'
print str.slice 6 ~ outputs 'World'

includes​

Checks whether the string contains the specified substring.

const str = 'Hello World'
print str.includes 'World' ~ outputs true
print str.includes 'Alia' ~ outputs false

indexOf​

Returns the position of the first occurrence of the substring.

const str = 'Hello World'
print str.indexOf 'o' ~ outputs 4
print str.indexOf 'World' ~ outputs 6

trim​

Removes whitespace from both ends of the string.

const str = ' Hello World '
print str.trim ~ outputs 'Hello World'

toUpperCase​

Returns the string converted to all uppercase.

const str = 'Hello World'
print str.toUpperCase ~ outputs 'HELLO WORLD'

toLowerCase​

Returns the string converted to all lowercase.

const str = 'Hello World'
print str.toLowerCase ~ outputs 'hello world'

toNumber​

Returns the numeric value corresponding to the string.

const str = '42'
print str.toNumber ~ outputs 42

toBoolean​

An empty string and '0' correspond to false; all other strings correspond to true.

const str = 'Hello'
print str.toBoolean ~ outputs true
const empty = ''
print empty.toBoolean ~ outputs false

toArray​

Returns an array consisting of the individual characters of the string.

const str = 'Hi'
print str.toArray ~ outputs ['H', 'i']

toString​

Returns the string itself.

const str = new String 'Hello'
print str.toString ~ outputs 'Hello'

valueOf​

Returns the primitive value of the string.

const str = new String 'Hello'
print str.valueOf ~ outputs 'Hello'

String Concatenation​

String concatenation is performed with the .. operator.

const str1 = 'Hello'
const str2 = 'World'
const result = str1 .. ' ' .. str2
print result ~ outputs 'Hello World'

Example Code​

const greeting = 'Hello, Alia!'
print greeting
print 'String length: ' .. greeting.length
const words = greeting.split ','
print 'Split result: ' .. words
const newGreeting = greeting.replace 'Alia' with 'World'
print 'After replacement: ' .. newGreeting
const part = greeting.slice 0 to 5
print 'Slice result: ' .. part
if greeting.includes 'Alia' { print 'String contains "Alia"' }
const pos = greeting.indexOf 'Alia'
print 'Position of "Alia": ' .. pos
const padded = ' Hello '
print 'After trimming: ' .. padded.trim

Next Steps​