Learn
Lesson 4

Strings

Join, slice, search and reshape text, and build strings from values with templates.

Text in OSL is a string. Write one with double or single quotes; the two mean the same thing, so pick whichever saves you escaping a quote inside:

main.osl
string a = "It's easy"
string b = 'She said "hi"'
string c = "Line one\nLine two"
log a
log b
log c
Click the code to edit it

A backslash escapes the next character: \n is a new line, \t a tab, \" a quote and \\ a backslash.

Joining text#

++ joins values into one string, and turns numbers and booleans into text on the way. + joins two strings too, but only when both sides are already strings, so ++ is the one to reach for:

main.osl
string name = "Ada"
int age = 36
log name ++ " is " ++ age
log "a" + "b"
log "ab" * 3
Click the code to edit it

Multiplying a string by a whole number repeats it.

Templates#

A string in backticks is a template: ${...} inside it runs the code and puts the result in the text. Templates are the clearest way to mix words and values:

main.osl
string name = "Ada"
int[] scores = [90, 85, 77]
log `${name} scored ${scores.sum()} in total`
log `Average: ${round(scores.sum() / scores.len)}`
Click the code to edit it

Any expression works inside ${...}, including calls, maths and other strings. Write \${ when you want the characters ${ themselves.

Length and positions#

.len is the number of characters. Positions start at 1, and negative positions count back from the end:

main.osl
string word = "OSL rocks"
log word.len
log word[1]
log word[-1]
log word.left(3)
log word.right(5)
log word.trim(5, 7)
Click the code to edit it

left(n) and right(n) take characters from either end, and trim(from, to) takes the characters between two positions, both included. Characters are whole Unicode characters, so "héllo".len is 5 and "🙂"[1] is the emoji.

Searching#

main.osl
string sentence = "The quick brown fox"
log sentence.contains("quick")
log sentence.startsWith("The")
log sentence.endsWith("dog")
log sentence.index("brown")
log sentence.count("o")
Click the code to edit it

index gives the position where the text first appears, or 0 when it doesn't. Because positions start at 1, a 0 can never be mistaken for "found at the start".

Changing text#

Strings never change in place. Each method returns a new string:

main.osl
string raw = "  Hello, World  "
log raw.trim()
log raw.trim().toUpper()
log raw.trim().toLower()
log raw.trim().replace("World", "OSL")
log "a-b-c".replace("-", "+")
log "a-b-c".replaceFirst("-", "+")
log "7".padStart("0", 3)
Click the code to edit it

replace changes every match, and replaceFirst only the first. padStart(padding, length) pads the front until the text is that long.

Splitting and joining#

split breaks a string into an array, and an array's join puts it back together:

main.osl
string csv = "red,green,blue"
string[] colours = csv.split(",")
log colours
log colours.len
log colours.join(" / ")
log "one two  three".split(" ")
Click the code to edit it

Arrays print as JSON, which is why colours shows with brackets and quotes. Notice the empty string where two spaces were next to each other.

Going through each character#

A for ... in loop walks a string one character at a time:

main.osl
int vowels = 0
for ch in "programming" (
  if "aeiou".contains(ch) (
    vowels++
  )
)
log vowels
Click the code to edit it

Multi-line text#

A template can span several lines, which is handy for messages and small files:

main.osl
string name = "Ada"
string letter = `Dear ${name},

Thanks for trying OSL.`
log letter
Click the code to edit it
Quick check

What does log "abc".index("z") print?

Quick check

Which line prints Score: 10?

Your turn

Initials

Print the initials of the full name, in capitals, with a dot after each.

main.osl
Ctrl Enter runs
Expected output
G.B.H.
0 of 24 lessons done