How to split one string about a fixed width, the string may come from the input or output cell of Notebook.

sample string

samplestring="{\"a,b,{4, 6, 8, 2, 1}\", \"a,b,{4, 2, 1, 10, 3}\", \"a,b,{2, 8, 7, 4}\", \\\r\n\"a,b,{8, 7, 9, 0, 5}\", \"a,b,{6, 9, 10, 3, 7}\", \"a,b,{2, 9, 9, 4, 2}\", \\\r\n\"a,b,{1}\", \"a,b,{0, 3, 4}\", \"a,b,{}\", \"a,b,{1, 5}\", \"a,b,{6, 10, 9, \\\r\n1, 8}\", \"a,b,{2, 7, 10}\", \"a,b,{3, 1, 2}\", \"a,b,{3, 10, 0}\", \"a,b,{2, \\\r\n1, 8, 6}\", \"a,b,{8, 5}\", \"a,b,{3, 8}\", \"a,b,{0, 10, 10, 5}\", \\\r\n\"a,b,{8}\", \"a,b,{10}\"}"

The tricky is to make substrings SyntaxQ True as much as possible.

And the final substring's StringLength should not greater than the fixed width unless width is too small.

My little try, seems not so easy as my first thought, maybe I'm too fool. And maybe someone asked before.

strGet[str_ , width_:20] :=
StringTake[str, spec = SyntaxLength[StringTake[str, width]]];
{strNew, strRest} = StringTake[str, {{1, spec}, {spec + 1, -1}}];


(*In [1]*):= strGet @ samplestring
Out[2]= {

One simple string split function without SyntaxQ

stringSplit[str_, width_] := Module[{strNew = str}, 
test = StringTake[strNew,
  {First[#], Last[#]} & /@ Partition[Range[StringLength @ strNew], width, width, 1, {}]]]

without SyntaxQ, see a example in bills answer of one of my prevous question. https://mathematica.stackexchange.com/a/26525/6648

My background is trying to copy Mathematica Code to SE with good form. So the following is related in the large direction.

Programmatically copy code so that all output is commented out

In this post, you can see clearly sample1 really with some fixed-width but not so well splitted. For example, string

"anything but", "around/round the clock" 

was splitted into

ound/round the clock"


  • $\begingroup$ You are still dumping poorly formatted code into your questions. Please make an effort to properly format your code, so that people can easily read it. $\endgroup$ – m_goldberg Jun 17 '13 at 3:39
  • $\begingroup$ @m_goldberg How about this edit, I went to fetch my lunch just now. $\endgroup$ – HyperGroups Jun 17 '13 at 3:47
  • 1
    $\begingroup$ @HyperGroups -- I really don't understand what you are after. It looks like you are trying to make functions that turn Mathematica into a word processor: this question (and the other that you refer to) are effectively "word wrap". Why is this sensible to do in Mathematica? There are many word processors available. $\endgroup$ – bill s Jun 17 '13 at 5:17
  • $\begingroup$ @bills en, something like "word wrap" and setting of PageWidth for Cell or the use of ExportTypesetOptions -> {"PageWidth" -> 90} here. meta.mathematica.stackexchange.com/questions/1015/… I'll take some time to consider whether the question is meaningless. Why I ask this is because some of my Copy Notebook To SE Post programs still no so good. $\endgroup$ – HyperGroups Jun 17 '13 at 5:26
  • $\begingroup$ @bills as you see the value of PageWidth, not one fixed value is suitable for some cases, maybe some dynamic values about the fixed value is the best if possible, for example. StringLength["{a,de,cde}"]=10, if the fixed value is 4,then, I'd like it splitted into {"{a,","de,","cde}", so the dynamic values are: {3,3,4} total to 10. $\endgroup$ – HyperGroups Jun 17 '13 at 5:40