Text
String methods cover search, trimming, reshaping, splitting and joining, and comparison; for repeated concatenation there is StringBuilder. Unless noted, methods are read-only and return new strings.
Search and checks
val s = "Hello, Mush";
print(s.Length, s.IsEmpty, " ".IsNullOrWhiteSpace());
print(s.Contains("mus", true), s.StartsWith("Hell"), s.EndsWith("xyz"));
print(s.IndexOf("o"), s.IndexOf("o", 5), s.LastIndexOf("o"));11 False True
True True False
4 -1 4Contains / StartsWith / EndsWith / IndexOf all take an optional ignoreCase argument (case-insensitive comparison). IndexOf accepts a start index and returns -1 on a miss.
Trimming and reshaping
val s = "Hello, Mush";
print(s.Substr(7), s.Substr(0, 5), s.Substring(7, 2));
print(s.ToUpper(), s.ToLower());
print(" hi ".Trim(), "hi".PadLeft(5, '.'), "ab".Repeat(3));
print(s.Replace("Mush", "World"));Mush Hello Mu
HELLO, MUSH hello, mush
hi ...hi ababab
Hello, WorldSubstr(start, length)runs to the end when length is omitted;Substringis an alias.Trim/TrimStart/TrimEndstrip whitespace.PadLeft/PadRightpad to the target width counting clusters.Repeat(n)repeats; a non-positive n yields the empty string.
Insertion, removal, and reversal
val s = "Hello, Mush";
print(s.Insert(5, "!!"), s.Remove(5));
print("abcdef".Reverse());Hello!!, Mush Hello
fedcbaRemove(start, length) deletes to the end when length is omitted. Reverse reverses in cluster order, so multi-cluster characters are not torn apart.
Single-cluster access
val s = "Hello, Mush";
print(s.CharAt(7), "é".CodePointAt(0));M 233CharAt(index)returns the index-th grapheme cluster (as astring); out of range reports MS6024.CodePointAt(index)returns the code point of the cluster’s first scalar.Get(index)is the corresponding fallible form: it fetches a cluster under the same grapheme-cluster semantics, failing withIndexOutOfRangewhen out of range.
val s = "héllo";
print(try? s.Get(1) ?? "?");
val t = "a🙂b";
print(try? t.Get(1) ?? "?");é
🙂CharAt / Get return a slice of the original string, so the result feeds straight into further string operations; for a single char value, use the indexer.
val family = "a👨👩👧b";
print(family[1] == '👨👩👧', family.CharAt(1) == "👨👩👧");True TrueSplitting and joining
print("a,b,,c".Split(","), "a,b,,c".Split(",", true));
print("1-2-3".Split("-"));
print(", ".Join(["a", "b"]));[a, b, , c] [a, b, c]
[1, 2, 3]
a, bSplit(separator) returns a string[], keeping empty segments by default; a second argument of true drops them. Join’s receiver is the separator, and its argument is the sequence (arrays and lists are both accepted).
Comparison and miscellany
print("abc".Compare("abd"), "b".Compare("a"));-1 1Compare returns a negative number, zero, or a positive number to express order, and Equals(other, ignoreCase) does equality checks. String ordering comparisons use ordinal semantics; see operators.
StringBuilder
Heavy + concatenation inside a loop produces piles of intermediate strings. StringBuilder accumulates the pieces in an internal buffer and materializes them once at the end with ToString():
import Text.*;
val sb = new StringBuilder();
sb.Append("one");
sb.AppendLine("");
sb.Append("two ");
sb.Append("x");
print(sb.ToString());
print(sb.Length);
sb.Clear();
print(sb.Length, sb.ToString() == "");one
two x
9
0 TrueAppend returns the builder itself, so calls chain; AppendLine(s) appends content plus a newline (the argument is required). StringBuilder lives in std’s Text module; to use the bare name you need import Text.*;.
Notes
- All indexes and lengths count grapheme clusters, not bytes or UTF-16 units — see Unicode and grapheme clusters.
- Numeric
ToString(format)formats share one table with interpolation; see string interpolation.
