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

时间:2019-09-20 21:42:36

标签: string parsing dm-script

我需要通过DM脚本在文本文件的一行中读取/加载一系列字符串,这些字符串之间有空格,空格的数量不是固定的,两个相邻字符串之间可以有8个空格,但是有7个空格在其他两个相邻字符串之间,我需要让DM知道,当它们遇到空格时,它是一个新字符串,但是如果连续遇到空格,它会在遇到非空格字符之前不计算新字符串。

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

2 个答案:

答案 0 :(得分:1)

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

enter image description here

您最可能需要的命令是

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.

您将需要使用while的{​​{1}}循环,只要找到find或到字符串末尾,循环就一直持续。

答案 1 :(得分:1)

这是一个示例脚本,用于显示如何解析以空格分隔的字符串:

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

enter image description here