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}}];
)
test
(*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
"ar ound/round the clock"
PageWidthfor Cell or the use ofExportTypesetOptions -> {"PageWidth" -> 90}here. http://meta.mathematica.stackexchange.com/questions/1015/about-newline-in-mathematica-seI'll take some time to consider whether the question is meaningless.
Why I ask this is because some of my
– HyperGroups Jun 17 '13 at 05:26Copy Notebook To SE Postprograms still no so good.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. – HyperGroups Jun 17 '13 at 05:40GraphicsBoxdata useful only when copied into Mathematica is probably not the way to go - your examples don't have to be so large that they must be formatted programmatically... – cormullion Jun 17 '13 at 08:12SyntaxQof True. I guess I've lost track of what the question really is. – bill s Jun 17 '13 at 09:07SyntaxQ=Trueand in my post, I use the wordas much as possible, sometimesSyntaxQ=False, but still could be a good split which I expected. For exampleSytaxLength["{a,"]=5which make the split also one possible split. And SyntaxQ in my post maybe a general meaning. – HyperGroups Jun 17 '13 at 09:18