Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Basic String Tests

Print

$ wo 'Print["Hello World!"]'
Hello World!
Null

StringLength

$ wo 'StringLength["Hello World!"]'
12

StringTake

$ wo 'StringTake["Hello World!", 5]'
Hello

StringDrop

$ wo 'StringDrop["Hello World!", 6]'
World!

StringJoin

$ wo 'StringJoin["Hello", " ", "World!"]'
Hello World!

StringSplit

$ wo 'StringSplit["Hello World!", " "]'
{Hello, World!}

StringStartsQ

$ wo 'StringStartsQ["Hello World!", "Hello"]'
True
$ wo 'StringStartsQ["Hello World!", "Bye"]'
False

StringEndsQ

$ wo 'StringEndsQ["Hello World!", "World!"]'
True
$ wo 'StringEndsQ["Hello World!", "Moon!"]'
False

StringPosition

Finds all positions of a substring, returning {start, end} pairs (1-indexed).

$ wo 'StringPosition["abcabc", "bc"]'
{{2, 3}, {5, 6}}
$ wo 'StringPosition["hello", "l"]'
{{3, 3}, {4, 4}}

StringMatchQ

Tests if string matches a pattern. * matches any sequence of characters (including empty). @ matches one or more characters, excluding uppercase letters.

Exact match:

$ wo 'StringMatchQ["hello", "hello"]'
True
$ wo 'StringMatchQ["hello", "world"]'
False
$ wo 'StringMatchQ["hello", "Hello"]'
False

Wildcard * matches any sequence:

$ wo 'StringMatchQ["hello", "h*o"]'
True
$ wo 'StringMatchQ["hello", "h*"]'
True
$ wo 'StringMatchQ["hello", "*o"]'
True
$ wo 'StringMatchQ["hello", "*"]'
True
$ wo 'StringMatchQ["hello", "h*l*o"]'
True
$ wo 'StringMatchQ["hello", "*ell*"]'
True

* can match empty sequence:

$ wo 'StringMatchQ["hello", "hello*"]'
True
$ wo 'StringMatchQ["hello", "*hello"]'
True
$ wo 'StringMatchQ["hello", "hel*lo"]'
True

Non-matching patterns:

$ wo 'StringMatchQ["hello", "h*x"]'
False
$ wo 'StringMatchQ["hello", "x*o"]'
False
$ wo 'StringMatchQ["hello", "hellooo"]'
False

Empty string cases:

$ wo 'StringMatchQ["", ""]'
True
$ wo 'StringMatchQ["", "*"]'
True
$ wo 'StringMatchQ["", "a"]'
False
$ wo 'StringMatchQ["hello", ""]'
False

Wildcard @ matches one or more non-uppercase characters:

$ wo 'StringMatchQ["hello", "h@o"]'
True
$ wo 'StringMatchQ["hello", "@"]'
True
$ wo 'StringMatchQ["hello", "h@"]'
True
$ wo 'StringMatchQ["hello", "@o"]'
True

@ requires at least one character (unlike *):

$ wo 'StringMatchQ["hello", "hello@"]'
False
$ wo 'StringMatchQ["", "@"]'
False

@ does not match uppercase letters:

$ wo 'StringMatchQ["hEllo", "h@o"]'
False
$ wo 'StringMatchQ["HELLO", "@"]'
False
$ wo 'StringMatchQ["helloWorld", "hello@"]'
False

@ matches lowercase, digits, and other non-uppercase characters:

$ wo 'StringMatchQ["hello123", "hello@"]'
True
$ wo 'StringMatchQ["hello world", "hello@"]'
True
$ wo 'StringMatchQ["a1b2c3", "@"]'
True

StringReverse

Reverses the characters in a string.

$ wo 'StringReverse["Hello"]'
olleH
$ wo 'StringReverse["abcde"]'
edcba
$ wo 'StringReverse[""]'

StringRepeat

Repeats a string n times.

$ wo 'StringRepeat["ab", 3]'
ababab
$ wo 'StringRepeat["x", 5]'
xxxxx
$ wo 'StringRepeat["hello", 0]'

StringTrim

Removes leading and trailing whitespace.

$ wo 'StringTrim["  hello  "]'
hello
$ wo 'StringTrim["  hello world  "]'
hello world

With a pattern, removes leading and trailing occurrences:

$ wo 'StringTrim["xxhelloxx", "xx"]'
hello

StringCases

Finds all occurrences of a substring.

$ wo 'StringCases["abcabc", "bc"]'
{bc, bc}
$ wo 'StringCases["hello", "l"]'
{l, l}
$ wo 'StringCases["hello", "x"]'
{}

ToString

Converts an expression to a string.

$ wo 'ToString[123]'
123
$ wo 'ToString[{1, 2, 3}]'
{1, 2, 3}
$ wo 'ToString[1 + 2]'
3

ToExpression

Converts a string to an evaluated expression.

$ wo 'ToExpression["1 + 2"]'
3
$ wo 'ToExpression["Plus[3, 4]"]'
7

StringPadLeft

Pads a string on the left to a specified length.

$ wo 'StringPadLeft["hi", 5]'
   hi
$ wo 'StringPadLeft["hi", 5, "0"]'
000hi
$ wo 'StringPadLeft["hello", 3]'
llo

StringPadRight

Pads a string on the right to a specified length.

$ wo 'StringPadRight["hi", 5, "0"]'
hi000
$ wo 'StringPadRight["hello", 3]'
hel