Flexible String Processing
Version 11 adds a variety of low-level functions that allow flexible manipulation of strings.
Take a string containing a sequence of personal details.
In[1]:=
![Click for copyable input](assets.en/flexible-string-processing/In_1.png)
string = "lastname Doe firstname John age 20 email john.doe@mail.com \
mobile 123-456-7890";
Construct a list of substrings with StringSplit.
In[2]:=
![Click for copyable input](assets.en/flexible-string-processing/In_2.png)
splitstring = StringSplit[string]
Out[2]=
![](assets.en/flexible-string-processing/O_1.png)
Pad all substrings with spaces on the right to make them be of length 20.
In[3]:=
![Click for copyable input](assets.en/flexible-string-processing/In_3.png)
padstring = StringPadRight[splitstring, 20, " "]
Out[3]=
![](assets.en/flexible-string-processing/O_2.png)
Partition the resulting expression to group strings by details.
In[4]:=
![Click for copyable input](assets.en/flexible-string-processing/In_4.png)
partstring = Partition[padstring, 2]
Out[4]=
![](assets.en/flexible-string-processing/O_3.png)
Use StringRiffle to concatenate all strings, inserting newlines between groups and empty strings within each group.
In[5]:=
![Click for copyable input](assets.en/flexible-string-processing/In_5.png)
rifstring = StringRiffle[partstring, "\n", ""]
Out[5]=
![](assets.en/flexible-string-processing/O_4.png)
The final expression is a String object.
In[6]:=
![Click for copyable input](assets.en/flexible-string-processing/In_6.png)
Head[rifstring]
Out[6]=
![](assets.en/flexible-string-processing/O_5.png)
An alternative way of inserting newlines is using the function InsertLinebreaks.
In[7]:=
![Click for copyable input](assets.en/flexible-string-processing/In_7.png)
StringTake[ExampleData[{"Text", "AliceInWonderland"}], 200]
Out[7]=
![](assets.en/flexible-string-processing/O_6.png)
In[8]:=
![Click for copyable input](assets.en/flexible-string-processing/In_8.png)
StringTake[ExampleData[{"Text", "AliceInWonderland"}], 200];
InsertLinebreaks[%, 40]
Out[8]=
![](assets.en/flexible-string-processing/O_7.png)