Wolfram: String Split

By Xah Lee. Date: .

String Split

StringSplit[ str ]

split a string to a list of strings.

StringSplit

(* split by whitespace *)
StringSplit[ "a b" ]
(* {a, b} *)
StringSplit[ str, patt ]

split a string by a pattern.

(* split by comma *)
StringSplit[ "a,b,c", "," ]
(* {a, b, c} *)
(* split by comma with optional space *)
StringSplit[ "a, b, c", RegularExpression[", +"] ]
(* {a, b, c} *)
StringSplit[ str, patt, n ]

max n items in result.

StringSplit[ "a-b-c-d-e", "-" , 3]
(* {a, b, c-d-e} *)
(* split by comma with optional space *)
StringSplit[ "a, b, c", RegularExpression[", +"] ]
(* {a, b, c} *)
StringSplit[ str, listOfPatterns ]

split a string by list of patterns.

StringSplit[ "Alice some.thing,water", {",", ".", " "} ]
(* {Alice, some, thing, water} *)
StringSplit[ str, pattern -> val ]

split a string by a pattern, and include the pattern in the result with val.

StringSplit[ "Alice some thing water", " " -> "and" ]
(* {Alice, and, some, and, thing, and, water} *)
StringSplit[ str, listOfRules ]

split a string by list of patterns, and include the patterns in the result with their replacement.

StringSplit[ "Alice some.thing,water",
{"," -> "plus", "." -> "and", " " -> "and"} ]
(* {Alice, and, some, and, thing, plus, water} *)
StringSplit[ stringList, p ]

work on multiple strings.

StringSplit[ {"a b c", "d e f"} ]
(* {{a, b, c}, {d, e, f}} *)

StringSplit[ {"a-b-c", "d-e-f"}, "-" ]
(* {{a, b, c}, {d, e, f}} *)

StringSplit[ {"a-b-c", "d-e-f"}, "-" -> "-"]
(* {{a, -, b, -, c}, {d, -, e, -, f}} *)

String to List of Characters

Characters[ str ]

to list of characters.

Characters[ "abc" ]
(* {a, b, c} *)

WolframLang String