5.0 KiB
String
A collection of UTF-16 characters.
Overview
Strings are a primitive datatype that contain a series of UTF-16 encoded characters.
To create a string use double quotes:
val greeting = "Hello there!"
// With type annotation
Str greeting = "Hello there!"
Misti doesn't allow string creation with single quotes <code>''</code>.
Strings can be concatenated with the plus +
operator:
val name = "John"
val greeting = "Hello"
val fullGreeting = greeting + " " + name + "!"
// fullGreeting = "Hello John!"
In the future, the language will support string concatenation and multi-line strings.
String comparison
To check if two strings are the same use the equal-to ==
operator.
"Hello" == "Hello" //: true
"Hello" == "hello" //: false
To check if two strings are different use the not-equal-to !=
operator.
"abc" != "123" //: true
"xyz" != "xyz" //: false
Comparing strings with <
, <=
, >
& >=
will compare each character of the string,
based on the UTF-16 value.
"a" < "b" //: true
"a" < "A" //: false
"aab" > "aac" //: false
"aab" < "aac" //: true
Automatic String conversion
Misti does not automatically convert other datatypes to String.
val sentence = "My age is: " + 20 // This will throw an error
// ^
// Cannot concatenate a String and a Number.
To do so use the method .toStr()
to explicitly convert to String.
val sentence = "My age is: " + 20.toStr() // ok
In the future, string interpolation will coerce datatypes into strings.
Escape characters
Misti supports the following escape characters:
-
\"
-
\\
-
\n
-
\r
-
\t
-
\b
API
Prelude's Str
contains methods and functions created for their usage within Misti.
To access the underlying JavaScript methods, use the js.String
module.
Constructor
class String[T](T value)
where T -> Printable
Creates a string from value
, by calling its toStr
method.
Parameters
T value
: Any value that implementsPrintable
, and therefore has atoStr
method.
Examples
val age = String(20) //: "20"
val condition = String(false) //: "false"
val numbers = Array(1, 2, 3) |> String //: "1,2,3"
Properties
length
Num length
Returns the number of UTF-16 code units in the string.
Example
val name = "John"
name.length //: 4
Methods
charAt
fun charAt(Num position) -> Str?
Returns the UTF-16 code unit located at position
.
Parameters
Num position
: An integer between0
and the string'slength - 1
.
Return
Str?
: The character atposition
, orNone
if it's invalid
Description
In a string with length
characters, this method returns the character (UTF-16 code point)
located at position
, if it is true that 0 <= position < length
.
If position
is out of range, this method will return None
.
Examples
val name = "John Doe"
name.charAt(0) //: Some("J")
name.charAt(1) //: Some("o")
name.charAt(7) //: Some("e")
name.charAt(-1) //: None
name.charAt(8) //: None
If you are sure that the position is valid, you can use the !!
operator to get
the value directly, instead of a Maybe
.
val greeting = "Hello!"
name.charAt(4)!! //: "o"
concat
fun concat[any T](T... values) -> Str
where T -> Printable
Concatenates the calling string and values
, and returns the result
as a new Str
Type parameters
any T
: Any datatype that implementsPrintable
Parameters
T... values
: Zero or more values of typeT
Return
Str
: A new string with the values concatenated
Description
concat
concatenates the callee, and any arguments passed to it in
a new string. The callee is not modified.
If the arguments don't have type Str
, they are converted with their
toStr
method.
Examples
val greeting = "Hello "
greeting.concat("world", "!") //: "Hello world!"
greeting.concat(123) //: "Hello 123"
greeting.concat(3, 2, 2) //: "Hello 322"
includes
fun includes(Str searchString, Num pos = 0) -> Bool
Returns whether the current string contains searchString
,
searching from position pos
Parameters
Str searchString
: The string to search forNum pos = 0
: The position from where to start searching.
Return
Bool
: true
if searchString is found, false
otherwise
Additional
If searchString
is the empty string ""
this method returns true
.
If pos
is negative, the search starts from the first character.
Examples
val loremIpsum = "Lorem ipsum dolor sit amet"
loremIpsum.includes("ipsum") //: true
loremIpsum.includes("ipsum", 10) //: false
loremIpsum.includes("ipsum", -5) //: true