Message Boards Message Boards

0
|
6318 Views
|
4 Replies
|
3 Total Likes
View groups...
Share
Share this post:

String Manipulation: How do you split a string of data?

Posted 10 years ago

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.

POSTED BY: Mark Sepulveda
4 Replies

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"}
POSTED BY: Nasser M. Abbasi
Posted 10 years ago

Just for fun:

StringCases[
 "955929400769971",
 Repeated[_, {3}]
 ]

{"955", "929", "400", "769", "971"}

POSTED BY: Kuba Podkalicki
Posted 10 years ago

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
POSTED BY: Gustavo Delfino
Posted 10 years ago

Thank you Nasser. That worked like a champ. Now to the docs to analyze what you did ;). I appreciate the assistance.

POSTED BY: Mark Sepulveda
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract