String patterns work very much like other patterns in the Wolfram Language, except that they operate on sequences of characters in strings rather than parts of expressions. In a string pattern, you can combine pattern constructs like _ with strings like "abc" using ~~.

StringCases["+string +patterns are +quite +easy", "+"~~_]

StringCases["+string +patterns are +quite +easy", "+"~~_~~_~~_]

StringCases["+string +patterns are +quite +easy", "+"~~x_->Framed[x]]

In a string pattern, _ stands for any single character. _ _ (“double blank”) stands for any sequence of one or more characters, and _ _ _ (“triple blank”) stands for any sequence of zero or more characters. _ _ and _ _ _ will normally grab as much of the string as they can.

StringCases["the [important] word", "["~~x__~~"]"->Framed[x]]

StringCases["now [several] important [words]", "["~~x__~~"]"->Framed[x]]

StringCases["now [several] important [words]", "["~~Shortest[x__]~~"]"->Framed[x]]

StringCases picks out cases of a particular pattern in a string. StringReplace makes replacements.

StringReplace["now [several] important [words]", {"["->"<<","]"->">>"}]

StringReplace["now [several] important [words]", "["~~Shortest[x__]~~"]":>ToUpperCase[x]]

NestList[StringReplace[#,{"A"->"AB","B"->"BA"}]&,"A",5]

StringMatchQ tests whether a string matches a pattern.

Select[WordList[ ],StringMatchQ[#,"a"~~___~~"b"]&]

You can use | and .. in string patterns just like in ordinary patterns.

StringCases["the AAA and the BBB and the ABABBBABABABA",("A"|"B")..]

In a string pattern, LetterCharacter stands for any letter character, DigitCharacter for any digit character and Whitespace for any sequence of “white” characters such as spaces.

StringCases["12 and 123 and 4567 and 0x456",DigitCharacter..]

StringCases["12 and 123 and 4567 and 0x456",Whitespace~~DigitCharacter..~~Whitespace]

It’s common in practice to want to go back and forth between strings and lists. You can split a string into a list of pieces using StringSplit.

StringSplit["a string to split"]

StringSplit["you+can+split--at+any--delimiter","+"|"--"]

Within strings, there’s a special newline character which indicates where the string should break onto a new line. The newline character is represented within strings as \n.

StringSplit["first line
second line
third line","\n"]

StringJoin joins any list of strings together. In practice, though, one often wants to insert something between the strings before joining them. StringRiffle does this.

StringRiffle[{"a","list","of","strings"},"---"]

In assembling strings, one often wants to turn arbitrary Wolfram Language expressions into strings. One can do this using TextString.

StringJoin["two to the ",TextString[50]," is ",TextString[2^50]]

A more convenient way to create strings from expressions is to use string templates. String templates work like pure functions in that they have slots into which arguments can be inserted.

StringTemplate["first `` then ``"][100,200]

StringTemplate["first: `a`; second `b`; first again `a`"][\[LeftAssociation]"a"->"AAAA", "b"->"BB BBB"\[RightAssociation]]

You can insert any expression within a string template by enclosing it with <*...*>. The value of the expression is computed when the template is applied.

StringTemplate["2 to the 50 is <* 2^50 *>"][ ]

StringTemplate["`1` to the `2` is <* #1^#2 *>"][2,50]

StringTemplate["the time now is <* Now *>"][ ]

Replace each space in "1 2 3 4" with "---". »

Get a sorted list of all sequences of 4 digits (representing possible dates) in the Wikipedia article on computers. »

Extract “headings” in the Wikipedia article about computers, as indicated by strings starting and ending with "===". »

Use a string template to make a grid of results of the form i+j=... for i and j up to 9. »

Find names of integers below 50 that have an “i” somewhere before an “e”. »

Make any 2-letter word uppercase in the first sentence from the Wikipedia article on computers. »

Make a labeled bar chart of the number of countries whose TextString names start with each possible letter. »

Find simpler code for 
Grid[Table[StringJoin[TextString[i],"^",TextString[j],"=",TextString[i^j]],{i,5},{j,5}]]. »

How should one read ~~ out loud?

It’s usually read “tilde tilde”. The underlying function is StringExpression.

How does one type `` to make a slot in a string template?

It’s a pair of what are usually called backquote or backtick characters. On many keyboards, they’re at the top left, along with ~ (tilde).

Can I write rules for understanding natural language?

Yes, but we didn’t cover that here. The key function is GrammarRules.

What does TextString do when things don’t have an obvious textual form?

It does its best to make something human readable, but if all else fails, it’ll fall back on InputForm.

Tech Notes