Strings are probably the most basic data type in all languages, yet they hold a very important value in ABS: considering that shell scripting is all about working around command outputs, we assume you will likely work a lot with them.
Strings are enclosed by double or single quotes:
"hello world"
'hello world'
You can escape quotes with a simple backslash:
"I said: \"hello world\""
or use the other quote to ease escaping:
'I said: "hello world"'
Their individual characters can be accessed with the index notation:
"hello world"[1] # e
Accessing an index that does not exist returns null.
You can also access a range of the string with the [start:end]
notation:
"string"[0:3] // "str"
where start
is the starting position in the array, and end
is
the ending one. If start
is not specified, it is assumed to be 0,
and if end
is omitted it is assumed to be the character in the string:
"string"[0:3] // "str"
"string"[1:] // "tring"
If end
is negative, it will be converted to length of string - end
:
"string"[0:-1] // "strin"
To concatenate strings, "sum" them:
"hello" + " " + "world" # "hello world"
Note that strings have what we call a "zero value":
a value that evaluates to false
when casted to boolean:
!!"" # false
To test for the existence of substrings within strings use the in
operator:
"str" in "string" # true
"xyz" in "string" # false
Double and single quoted strings behave differently if the string contains
escaped special ASCII line control characters such as LF "\n"
, CR "\r"
,
and TAB "\t"
.
If the string is double quoted these characters will be expanded to their ASCII codes. On the other hand, if the string is single quoted, these characters will be considered as escaped literals.
This means, for example, that double quoted LFs will cause line feeds to appear in the output:
⧐ echo("a\nb\nc")
a
b
c
⧐
Conversely, single quoted LFs will appear as escaped literal strings:
⧐ echo('a\nb\nc')
a\nb\nc
⧐
And if you need to mix escaped and unescaped special characters, then you can do this with double escapes within double quoted strings:
⧐ echo("a\\nb\nc")
a\\nb
c
⧐
Unicode characters are supported in strings:
⧐ echo("⺐")
⺐
⧐ echo("I ❤ ABS")
I ❤ ABS
Special characters also work with split()
and join()
and other string functions as well.
- Double quoted expanded special characters:
⧐ s = split("a\nb\nc", "\n")
⧐ echo(s)
[a, b, c]
⧐ ss = join(s, "\n")
⧐ echo(ss)
a
b
c
⧐
- Single quoted literal special characters:
⧐ s = split('a\nb\nc', '\n')
⧐ echo(s)
[a, b, c]
⧐ ss = join(s, '\n')
⧐ echo(ss)
a\nb\nc
⧐
- Double quoted, double escaped special characters:
⧐ s = split("a\\nb\\nc", "\\n")
⧐ echo(s)
[a, b, c]
⧐ ss = join(s, "\\n")
⧐ echo(ss)
a\\nb\\nc
⧐
Returns the length of a string:
"hello world".len() # 11
Formats a string (sprintf convention):
"hello %s".fmt("world") # "hello world"
Converts a string to a number, if possible:
"99.5".number() # 99.5
"a".number() # ERROR: int(...) can only be called on strings which represent numbers, 'a' given
Checks whether a string can be converted to a number:
"99.5".is_number() # true
"a".is_number() # false
Use this function when "...".number()
might return an error.
Converts a string to a number, and then rounds it down to the closest integer. The string must represent a number.
"99.5".int() # 99
"a".int() # ERROR: int(...) can only be called on strings which represent numbers, 'a' given
Converts a string to a number, and then rounds the number with the given precision.
The precision argument is optional, and set to 0
by default.
The string must represent a number.
"10.3".round() # 10
"10.6".round() # 11
"10.333".round(1) # 10.3
"a".round() # ERROR: round(...) can only be called on strings which represent numbers, 'a' given
Converts a string to a number, and then rounds the number up to the closest integer.
The string must represent a number.
"10.3".ceil() # 11
"a".ceil() # ERROR: ceil(...) can only be called on strings which represent numbers, 'a' given
Converts a string to a number, and then rounds the number down to the closest integer.
The string must represent a number.
"10.9".floor() # 10
"a".floor() # ERROR: floor(...) can only be called on strings which represent numbers, 'a' given
Splits a string by separator:
"1.2.3.4".split(".") # ["1", "2", "3", "4"]
Splits a string by newline:
"first\nsecond".lines() # ["first", "second"]
Parses the string as JSON, returning a hash:
⧐ s = '{"a": 1, "b": "string", "c": true, "d": {"x": 10, "y": 20}}'
⧐ h = s.json()
⧐ h
{a: 1, b: string, c: true, d: {x: 10, y: 20}}
⧐ h.d
{x: 10, y: 20}
This function is deprecated and might be removed in future versions.
Use the "in" operator instead:
"str" in "string"
Checks whether str
is present in the string:
"string".contains("ing") # true
"string".contains("ong") # false
Identity:
"string".str() # "string"
Checks whether any of the characters in str
are present in the string:
"string".any("abc") # true
"string".any("xyz") # false
Checks whether the string has the given prefix:
"string".prefix("str") # true
"string".prefix("abc") # false
Checks whether the string has the given suffix:
"string".suffix("ing") # true
"string".suffix("ong") # false
Creates a new string, repeating the original one i
times:
"string".repeat(2) # "stringstring"
Replaces occurrences of x
with y
, n
times.
If n
is omitted, or negative, it will replace all occurrencies:
"string".replace("i", "o", -1) # "strong"
"aaaa".replace("a", "x") # "xxxx"
"aaaa".replace("a", "x", 2) # "xxaa"
You can also replace an array of characters:
"string".replace(["i", "g"], "o") # "strono"
Titlecases the string:
"hello world".title() # "Hello World"
Lowercases the string:
"STRING".lower() # "string"
Uppercases the string:
"string".upper() # "STRING"
Removes empty spaces from the beginning and end of the string:
" string ".trim() # "string"
Removes str
from the beginning and end of the string:
"string".trim_by("g") # "strin"
Returns the index at which str
is found:
"string".index("t") # 1
Returns the last index at which str
is found:
"string string".last_index("g") # 13
This function is deprecated and might be removed in future versions.
Use the index notation instead:
"string"[0, 3]
Returns a portion of the string, from start
to end
:
"string".slice(0, 3) # "str"
If start
is negative, it slices from the end of the string,
back as many characters as the value of start
:
"string".slice(-3, 0) # "ing"
That's about it for this section!
You can now head over to read about numbers.