I am fairly new to mathematica. I am trying to split a string and output it as triplets.
I have tried playing with the StringSplit, Split, and Partition functions to no avail.
Here is a sample:
[IN] “955929400769971”
[OUT] 955 929 400 769 971
Any ideas would be appreciated.
nma
(Nasser M. Abbasi)
January 24, 2015, 10:08pm
2
one way might be
str = "955929400769971";
c = Partition[Characters[str], 3];
c = Insert[#, " ", -1] & /@ c;
StringJoin[StringJoin[#] & /@ c]
Which gives
"955 929 400 769 971 "
And if you just want the strings in a list:
str = "955929400769971";
c = Partition[Characters[str], 3];
StringJoin[#] & /@ c
gives
{"955", "929", "400", "769", "971"}
Thank you Nasser. That worked like a champ. Now to the docs to analyze what you did ;). I appreciate the assistance.
kubapod
(Kuba Podkalicki)
January 25, 2015, 12:00am
4
Just for fun:
StringCases[
"955929400769971",
Repeated[_, {3}]
]
{“955”, “929”, “400”, “769”, “971”}
gdelfino
(Gustavo Delfino)
January 25, 2015, 5:01pm
5
This is similar to Nasser’s answer, but using Riffle instead of Insert:
In[1]:= StringJoin@Riffle[Partition[Characters@"955929400769971",3]," "]
Out[1]= 955 929 400 769 971