我正在處理一個小的函數,該函數應該在子字符串中拆分字符串,以確保沒有任何字將被切割。在沒有拆分詞的子字符串(行)中拆分字符串
讓我們這個用例:
「事情錯了,我想不出有什麼問題」
總字符: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;
}
on line for(var i:Number = 0; i
實際上,當我複製我的代碼我已經改變了一些變量名稱,我忘記從o替換該實例輸出。已經對上面的代碼進行了更正。謝謝 – Lothre1