Questions tagged [string-manipulation]

Questions on the manipulation of String objects in Mathematica, and the functions used for these manipulations.

Questions on the manipulation of String objects in Mathematica, and the functions used for these manipulations. This includes converting (ToString), joining (StringJoin), cutting (StringSplit), modifying (StringInsert, StringDrop, StringTake, etc.), matching, selecting or replacing parts of (StringMatchQ, StringReplace, StringPosition, etc.) a string or list of strings.

Note that string-specific functions are named after their list-manipulating counterparts with the String... prefix added (cf. Cases -> StringCases, MatchQ -> StringMatchQ). Almost all list-based function has its string-equivalent.

Add if the question is about matching regular expressions in strings. Use for importing/exporting string/text format data, textual analysis and data mining of text. Questions about how to encode strings in a certain encoding should use .

Useful links

Example questions

1280 questions
59
votes
5 answers

How to join two Style[]d strings

Does anyone know whether it is possible to combine\join two styled strings? That is, while the following code works fine: omega = "text"; omega<>omega when I try to join my omegas into one string but having different colors like…
iav
  • 2,006
  • 17
  • 26
32
votes
9 answers

Partition string into chunks

This seems like it should be trivial, but how do I partition a string into length n substrings? I can of course write something like chunk[s_, n_] := StringJoin[#] & /@ Partition[Characters[s], n] so that chunk["ABCDEF",2] -> {"AB","CD","EF"} but…
David G
  • 629
  • 4
  • 6
21
votes
7 answers

How to capitalize the first letter of each word in a string

The following examples only work for the first character of the whole string, but not of words. s = "words are lowercase"; StringJoin[MapAt[ToUpperCase, Characters[s], 1]] (* ==> "Words are lowercase" *) StringReplacePart[#,…
HyperGroups
  • 8,619
  • 1
  • 26
  • 63
17
votes
5 answers

Opposite of StringSplit

What is an appropriate command that does the opposite of the following? StringSplit["a b c d e f g"," "]
William
  • 7,595
  • 2
  • 22
  • 70
17
votes
9 answers

How do I extract a number from a string?

I want to extract the number from an alphanumeric string. This is what I tried: StringTake["thiru3", {6, 6}] The result I got is 3, but it is still a String, which I determined by evaluating: NumberQ[StringTake["thiru3", {6, 6}]] which returns…
thirupathi
  • 193
  • 1
  • 1
  • 4
16
votes
6 answers

Type conversions

I have the following list, each element of which is of type string: {"abc", "def","2","ghi","7"} Is there an efficient way to arrive at: {"abcdef", 2,"ghi",7} where adjacent alphabetic elements are combined and string representations of integers…
Suite401
  • 4,793
  • 8
  • 18
15
votes
3 answers

How to remove accents from text?

I would like to know how I can remove accents from a string. For example, how can I transform "string test áéíóú" into "string test aeiou"? I have to normalize some text to make comparisons, and this would be very helpful.
Murta
  • 26,275
  • 6
  • 76
  • 166
13
votes
7 answers

How to convert string to integer list?

Saying we have a string: "1 2 3 4 5 6" I want to convert this to integer list. First I do: In[82]:= str = StringReplace["1 2 3 4 5 6", " " -> ","] // List Out[82]= {"1,2,3,4,5,6"} No I need to convert Out[82] to be just {1,2,3,4,5,6}. Any…
SuTron
  • 1,708
  • 1
  • 11
  • 21
13
votes
2 answers

How to get string representation (like repr in Python)

I have a string variable. I want to obtain another string which contains the representation of the string variable content itself. s = "a \n b" I need to get a string containing the exact characters from above, including the escape character \ and…
Meh
  • 1,637
  • 2
  • 13
  • 17
13
votes
3 answers

How complete is the WL's string language?

Consider the following example: Suppose I am interested in words that contain all the letter of the list characters={"e","t","l","a","b"} at least once. An example would be the word "abilities" or a more minimalist word would be "table". If I try to…
user13892
  • 9,375
  • 1
  • 13
  • 41
13
votes
5 answers

How does string interpolation work in Mathematica

I've noticed that Mathematica 8 seems to have some kind of string interpolation feature, but I couldn't find any documentation on it and I can't figure out how it works. For example, if I enter StringForm["x=``", 1 + 2] in a notebook and evaluate…
Niki Estner
  • 36,101
  • 3
  • 92
  • 152
12
votes
5 answers

How can I generate a list of combinations of string joins?

Suppose I have the string lists {'a','b','c'} and {'1','2','3'}. How do I get Mathematica to generate the list {'a1','a2','a3','b1','b2','b3','c1','c2','c3'}?
Kim Fierens
  • 1,857
  • 1
  • 16
  • 18
11
votes
1 answer

Named string patterns in Alternatives, why does it depend on order?

When using the same name for part of patterns inside Alternatives how come it behaves differently for strings than in normal patterns? {StringMatchQ["ab", (x_ ~~ "c") | ("a" ~~ x_)], StringMatchQ["ab", ("a" ~~ x_) | (x_ ~~ "c")]} (* False, True…
ssch
  • 16,590
  • 2
  • 53
  • 88
10
votes
1 answer

Naming a pattern changes outcome in StringCases[]

s = "1 2 "; StringCases[s, (n : NumberString ~~ " ") .. ] StringCases[s, (NumberString ~~ " ") .. ] yields {"1 ", "2 "} {"1 2 "} Why?
mrupp
  • 777
  • 4
  • 9
10
votes
5 answers

Caesar's cipher

How can Caesar cipher be implemented? I want to use StringReplace but I don't know how to write the replacement rule to replace the characters with, say, the character two positions down in the alphabet.
Whelp
  • 1,715
  • 10
  • 21
1
2 3
15 16