2

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"

https://mathematica.stackexchange.com/questions/27080/how-to-look-up-the-dictionary-for-some-bad-form-phrase

HyperGroups
  • 8,619
  • 1
  • 26
  • 63
  • 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. – m_goldberg Jun 17 '13 at 03:39
  • @m_goldberg How about this edit, I went to fetch my lunch just now. – HyperGroups Jun 17 '13 at 03:47
  • 1
    @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. – bill s Jun 17 '13 at 05:17
  • @bills en, something like "word wrap" and setting of PageWidth for Cell or the use of ExportTypesetOptions -> {"PageWidth" -> 90} here. http://meta.mathematica.stackexchange.com/questions/1015/about-newline-in-mathematica-se

    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.

    – HyperGroups Jun 17 '13 at 05:26
  • @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. – HyperGroups Jun 17 '13 at 05:40
  • The ideal question on Mathematica SE is short, with simple and minimal code examples, a clear description of the problem, with no unnecessary detail or elaborate formatting. A 6000+ character dump or compressed GraphicsBox data 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:12
  • Your latest code edit is a great improvement. However, the normal way to handle comma-delimited sequences is "a, b, c"; that is, expression-comma-space-expression-comma-space-... – m_goldberg Jun 17 '13 at 08:43
  • @HyperGroups -- in your comment, you say you would like to split it into things like "{a," and "de,". But none of these have SyntaxQ of True. I guess I've lost track of what the question really is. – bill s Jun 17 '13 at 09:07
  • @bills sorry for that, I should think how to modify the question with a clear description. So your comments are important to me. That is my non-rigorous, that example in my comment is show for the purpose, does not suit to the SyntaxQ=True and in my post, I use the word as much as possible, sometimes SyntaxQ=False, but still could be a good split which I expected. For example SytaxLength["{a,"]=5 which make the split also one possible split. And SyntaxQ in my post maybe a general meaning. – HyperGroups Jun 17 '13 at 09:18
  • @m_goldberg ok, I'll continue to make effort on formating codes. – HyperGroups Jun 17 '13 at 09:19
  • @cormullion ok, learned that and agree that. – HyperGroups Jun 17 '13 at 09:24

0 Answers0