如何在 DM 脚本中按空格拆分字符串

How to split strings by spaces in DM script

我需要通过DM脚本在文本文件的一行中read/load一系列字符串,字符串之间有spaces,spaces的数量是不固定,两个相邻字符串之间可能有 8 spaces,但其他两个相邻字符串之间有 7 spaces,我需要让 DM 知道当他们遇到 space 时,它是新字符串,但如果连续遇到spaces,则不算新字符串,直到遇到非space个字符。

我很感激任何建议。谢谢,

这是标准的字符串操作。您需要构建自己的方法来使用 "Objects:String Object" 下 F1 帮助中记录的可用字符串命令来解析字符串,我认为甚至有一个关于解析的示例:

您最可能需要的命令是

Number len( String str )
Returns the length of a String.

String left( String str, Number count )
Returns leftmost count characters of a string.

String mid( String str, Number offset, Number count )    
Returns count characters of a string starting at offset.

String right( String str, Number count )    
Returns rightmost count characters of a string.

Number find( String s1, String s2 )    
Returns the index of the first occurrence of the substring 's2' in 's1', or '-1' if there is no such occurrence.

您需要一个使用 findwhile 循环,只要找到 " " 或一直到字符串末尾,该循环就会继续。

这是一个示例脚本,展示了如何解析 space 分隔的字符串:

TagGroup ParseText( string inputString ) {
    // initialize a tag list to store parsing results
    TagGroup tgWordList = NewTagList();
    //
    while( inputString.len() > 0 ) {
        number pos = inputString.find( chr(32) );
        if( pos > 0 ) {         // "space" (i.e. ASC code: 32) is found and it's not the leading character
            string str = inputString.left( pos );
            tgWordList.TagGroupInsertTagAsString( tgWordList.TagGroupCountTags(), str );
            inputString = inputString.right( inputString.len() - pos );
        }
        else if( pos == 0 ) {   // first chracter is "space"
            inputString = inputString.right( inputString.len() - 1 );
            if( inputString == chr(32) ) break;
        }
        else {                  // no "space" found in whole string
            tgWordList.TagGroupInsertTagAsString( tgWordList.TagGroupCountTags(), inputString );            
            break;
        };
    };
    return tgWordList;
};

string test = "how the DM script recognize the spaces between the strings";
TagGroup tg = test.parseText();
tg.TagGroupOpenBrowserWindow(0);