Note that there are some explanatory texts on larger screens.

plurals
  1. POBreak a string in substrings (lines) without split words
    primarykey
    data
    text
    <p>i'm working in a small function that should break a string in substrings making sure that none of words will be cutted.</p> <p>Let's get this use case: </p> <p><strong>"something is going wrong and i can't figure out what's the problem"</strong></p> <p>total characters: 63;</p> <p><strong>with a max chars length of 15 per line i would get this substr "something is go"</strong></p> <p><strong>as you can see it is cutting the word when i would like to get the entire word.</strong></p> <p>line[1]: something is //break the string on the last space bellow the max chars length;</p> <p>line[2]: going wrong .... //includes's the missing characters from the first line at the begining of the second line </p> <p>line[3]: .... //and so on until all the characters get looped.</p> <p>I come up with this solution which is driving me crazy. </p> <pre><code>function splitString(obj:Object):Array{ if (obj.hasOwnProperty("d_string")) { var texto:String = obj.d_string; var textoStr:Array = texto.split(""); var textoLen:Number = texto.length; trace("textoLen " + textoLen); var maxCharPerLine:Number; if (obj.hasOwnProperty("d_limit")){ maxCharPerLine = obj.d_limit; trace("maxCharPerLine" + maxCharPerLine); }else{ maxCharPerLine = 20; } var textLine:Array = []; var currentLine:Number = 1; var currentIndex:Number = 0; var cachedCharsForLine:Array = []; //all characters between the range var lastCachedCharsForLine:Array = []; //mirror of all characters stoping at the last space found var missingChars:Array = []; var canCleanData:Boolean = false; /*START LOOPING OVER THE STRING*/ for (var i:Number = 0; i&lt; textoLen; i++) { //&lt;block1&gt; if ( currentIndex == maxCharPerLine || i == textoLen -1 ){ canCleanData = true; } //&lt;block2&gt; 22 if ( currentIndex &lt;= maxCharPerLine ){ cachedCharsForLine.push(textoStr[i]); //trace(cachedCharsForLine); } trace(textoStr[i]); trace(textoStr[i] == " "); /*is the characters a space?*/ if (textoStr[i] == " ") { /*1. even after the condition above returns false*/ lastCachedCharsForLine = []; lastCachedCharsForLine = cachedCharsForLine; } /*as you can see from the output this value is being updated every iteration after the first space get found when it was suppose to be updated only if the a char of type &lt;space&gt; get found" */ trace("-----------------" + lastCachedCharsForLine) //&lt;block4&gt; if( currentIndex == maxCharPerLine || i == textoLen -1 ){ if (textoStr[i]==" " || textoStr[i+1]==" "){ trace("\n A"); //trace("@@@ " + lastCachedCharsForLine); textLine[currentLine] = lastCachedCharsForLine; trace("//" + textLine[currentLine]); } else{ trace("\n B"); //trace("@@@ " + lastCachedCharsForLine); textLine[currentLine] = lastCachedCharsForLine; trace("//" + textLine[currentLine]); } currentIndex = 0; currentLine ++; } //&lt;block5&gt; if (currentLine &gt; 1 &amp;&amp; canCleanData){ trace("DATA CLEANED"); canCleanData = false; cachedCharsForLine = []; lastCachedCharsForLine = []; } currentIndex ++; } return textLine; }else{ return textLine[0] = false; } } var texto:String = "Theres something going wrong and it's driving me crazy."; var output:Array = [] output = splitString({"d_string":texto,"d_limit":15}); for (var i:Number = 0; i&lt;output.length; i++){ trace(output[i] + "\n\n"); } </code></pre> <p>lastCachedCharsForLine's variable is being updated as you can see from this trace line.</p> <pre><code>trace("-----------------" + lastCachedCharsForLine) </code></pre> <p>even with the condition bellow returning false</p> <pre><code> if (textoStr[i] == " ") { /*1. even after the condition above returns false*/ lastCachedCharsForLine = []; lastCachedCharsForLine = cachedCharsForLine; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload