2

私は小さな関数で作業しており、文字列を部分文字列に分割して、単語が切り取られないようにする必要があります。

このユースケースを取得しましょう:

「何かがうまくいかず、何が問題なのか理解できません」

合計文字数:63;

1行あたりの最大文字長が15の場合、このsubstr「somethingisgo」を取得します。

ご覧のとおり、単語全体を取得したいときに単語を切り取っています。

line [1]:何かが//最大文字長より下の最後のスペースの文字列を壊します。

line [2]:うまくいかない....//2行目の先頭にある最初の行から欠落している文字を含む

line [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;
                }
4

2 に答える 2

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'));

        }
    }
}
于 2012-07-02T17:51:24.903 に答える
2

StringUtilsGrant Skinnerの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, "...");
于 2012-07-02T17:55:40.157 に答える