WolframLang: String

By Xah Lee. Date: . Last updated: .

Quote String

String is quoted by double straight quote.

"this is a string"

string can be multi-line, can include any unicode characters:

x = "i ♥ cats
and dogs"

String Escapes

\n
newline char in string.
\t
tab char in string.
\"
double quote in string.

example:

"this is\na string"

"Said \"Yes\""

String Functions

There are about 100 functions on string. Here's some of the most basic ones:

length

StringLength
return the number of chars. StringLength
StringLength["abc"]
(* 3 *)

join string

StringJoin[s1, s2]
Short syntax: s1 <> s2
StringJoin
StringJoin["ab", "cd", "e"]
(* "abcde" *)
"ab" <> "cd" <> "e"
(* "abcde" *)

Get Substring, by Position

StringTake[str, n]
first char to nth. StringTake
StringTake["abcde", 3] === "abc"
StringTake[str, -n]
count from right, first char to nth.
StringTake["abcde", -3] === "cde"
StringTake[str, {n}]
nth char.
StringTake["abcde", {3}] === "c"
StringTake[str, {n, m}]
nth to mth chars.
StringTake["abcde", {2,4}] === "bcd"
StringTake[str, {spec1, spec2 etc}]
return a list of result, for each spec.
StringTake["abcde", {2, {2}, {2,4}}] === {"ab", "b", "bcd"}
StringTake[{str1, str2 etc}, spec]
return a list of result, for each string.
StringTake[{"abcde", "123456"}, 2] === {"ab", "12"}
StringTake[{"abcde", "123456"}, {2}] === {"b", "2"}
StringTake[{"abcde", "123456"}, {2,4}] === {"bcd", "234"}
StringTake[{"abcde", "123456"}, {2, {2}, {2,4}}] ===
{{"ab", "b", "bcd"}, {"12", "2", "234"}}

Get Substring by Pattern

StringCases
Get substring, by String pattern (regex) . Return a List . Use Part to get the elements. StringCases
(* extract email address *)

x = "text contain emails john@gmail.com and mary441@yahoo.com etc";

StringCases[ x, RegularExpression[ "[a-z0-9]+@[a-z0-9]+\\.com" ] , IgnoreCase -> True]

(* {"john@gmail.com", "mary441@yahoo.com"} *)

String to list

StringSplit
split a string to a list of strings. StringSplit
(* split by whitespace *)
StringSplit[ "a b" ]
(* {"a", "b"} *)
(* split by comma *)
StringSplit[ "a,b,c", "," ]
(* {"a", "b", "c"} *)

List to string

StringRiffle
List to string. StringRiffle
StringRiffle[ {"a" , "b" , "c"}, " " ]
 (* "a b c" *)

Insert into string

StringInsert
Insert by position StringInsert
StringInsert[ "love cat", "I ", 1 ]
(* I love cat *)

delete substring

StringDrop
Delete parts by position. StringDrop
StringDelete
Delete parts by String pattern (regex) . StringDelete
StringTrim
Remove whitespace StringTrim

search, test, pattern matching

Note: function name that ends in Q means it always return one of the builtin symbols True or False. Q is a reminder for “Question”. [see WolframLang: True/False (boolean)]

Many of the following function takes a String pattern (regex) .

StringQ
Check if is string. StringQ
StringStartsQ
StringStartsQ
StringEndsQ
StringEndsQ
StringMatchQ
StringMatchQ
StringContainsQ
StringContainsQ[ "some thing", "th" ]
(* True *)
StringContainsQ
StringFreeQ
StringFreeQ
StringPosition
StringPosition
StringCount
Count occurance StringCount

string replace

StringReplace
replace string by String pattern (regex) . StringReplace
StringReplace[ "some dogs" , {"dog" -> "cat"}]

(* "some cats" *)
StringReplacePart
replace string by position. StringReplacePart
StringReplacePart[ "some dogs", "cat", {6, 8}]
(* "some cats" *)

format string

ToString
Convert number or any expression to string. ToString
StringTemplate
Template system with placeholders StringTemplate

String pattern

String pattern (regex)

List All String Functions

WolframLang: List Functions, Find Function

WolframLang in Depth

Basics

Comment, Print

String

Arithmetic

List

Expression, Atom, Head

Boolean

Conditional

Variable

Loop

Data Structure

Function

Pattern Matching

Advanced

Shell Tasks

Misc