2012-07-02 124 views
2

我正在處理一個小的函數,該函數應該在子字符串中拆分字符串,以確保沒有任何字將被切割。在沒有拆分詞的子字符串(行)中拆分字符串

讓我們這個用例:

「事情錯了,我想不出有什麼問題」

總字符:63;

爲15的最多字符長度每行,我會得到這個SUBSTR「事情是去」

,你可以看到它被割字時,我想獲得整個單詞。

line [1]:something is // break the string on the last space bellow the max chars length;

線[2]:去錯.... //包括的從第一行的缺少的字符在第二條線的開頭

線[3]:.... //等直到所有角色都變成了環狀。

我想出了這個讓我瘋狂的解決方案。

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< textoLen; i++) 
    { 
     //<block1> 
     if (currentIndex == maxCharPerLine || i == textoLen -1){ 
      canCleanData = true; 
     } 
     //<block2> 22 
     if (currentIndex <= 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 <space> get found" 
      */ 
      trace("-----------------" + lastCachedCharsForLine) 


     //<block4> 
     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 ++; 
     } 

     //<block5> 
     if (currentLine > 1 && 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<output.length; i++){ 
    trace(output[i] + "\n\n"); 
} 

lastCachedCharsForLine的變量被更新,你可以從這個軌跡線看。

trace("-----------------" + lastCachedCharsForLine) 

即使條件波紋管返回false

if (textoStr[i] == " ") { 
        /*1. even after the condition above returns false*/ 
        lastCachedCharsForLine = []; 
        lastCachedCharsForLine = cachedCharsForLine; 
       } 
+0

on line for(var i:Number = 0; i

+0

實際上,當我複製我的代碼我已經改變了一些變量名稱,我忘記從o替換該實例輸出。已經對上面的代碼進行了更正。謝謝 – Lothre1

回答

4

這是有點樂趣。下面的代碼輸出:

|-------------| 
something is 
going wrong 
and i can't 
figure out 
what's the 
problem. 
WORDTHATISWAYTOOLONGFORTHEBUFFER. 
A quick brown 
fox jumped 
over the lazy 
dog. 

package { 

    import flash.display.Sprite; 

    final public class Test2 extends Sprite { 

     public function Test2() { 

      splitIntoLines("something is going wrong and i can't figure out what's the problem. WORDTHATISWAYTOOLONGFORTHEBUFFER. A quick brown fox jumped over the lazy dog."); 

     } 

     private function splitIntoLines(text:String, maxLineLength:int = 15):void { 
      const words:Array = text.split(/\s+/); 
      const output:Array = []; 

      var buffer:String = ''; 
      while (words.length) { 

       var word:String = words.shift() as String; 

       // if buffer has something, add a space 
       if (buffer.length) { 

        if (buffer.length + word.length + 1 < maxLineLength) { 
         buffer += ' ' + word; 
        } else { 
         output.push(buffer); 
         buffer = word; 
        } 

       // otherwise, it's the first word 
       } else { 

        if (buffer.length + word.length < maxLineLength) { 
         buffer += word; 
        } else { 
         output.push(buffer); 
         buffer = word; 
        } 
       } 
      } 

      // something is still in there? 
      if (buffer.length) { 
       output.push(buffer); 
      } 

      trace(output.join('\n')); 

     } 
    } 
} 
+0

這不是一個快速的解決方案,它只是* a *解決方案。 – ansiart

+0

非常感謝你ansiart。 我花了整個下午試圖達到這個結果。這是令人難以置信的速度有多快。 – Lothre1

+0

和一個小車。但我仍然可以建立在您的代碼上。 – Bitterblue

2

它可能會讓你的生活更容易,如果你使用格蘭特斯金納StringUtils LIB:

http://gskinner.com/blog/archives/2007/04/free_extension.html

truncate方法是你在找什麼:

var line:String = "Something is going wrong and I can't figure out what's the problem." 
var truncatedLine:String = StringUtils.truncate(line, 15, "..."); 
+0

非常感謝你的ilollar。一個非常有用的字符串utils庫。 – Lothre1