如何用“ Multiple”标记分割字符串,然后一一处理

我正在寻找一种将多个标签的字符串拆分为单独的字符串以逐一处理的方法 例如,当字符串og标签“ [{M} sometag] [sometag] [{F} sometag]”即将到来时,应首先将其与“ [”分开,然后仅保留{M} sometag并逐个处理。有什么主意吗?

Swdream2008 回答:如何用“ Multiple”标记分割字符串,然后一一处理

以下是伪代码为您提供指导:

我们必须仔细检查每个字符,并在字符串后附加“结果”,如果发现任何看起来合适的字符

需要布尔值/布尔值字段,例如“ isCandidate”,并将其初始设置为false

  1. 检查当前字符是否不是'['

    i。检查当前字符的前一个字符是否为'['

        a. Append previous character (i.e. '[') to result
    
        b. Set isCandidate to true
    

    ii。检查isCandidate是否为真

    a. Check if current character is not a ']'
    
        1) Append current character (i.e. anything not a '[' or ']') to result
    
    b. Check if current character is a ']'
    
        1) Append current character (i.e. ']') to result
    
        2) Set isCandidate to false (You have found your first word closed within '[' and ']')
    
        3) Add the result string in a list or array (or any structure where you'd be maintaining your results)
    
        4) Set result to empty string
    
  2. 重复下一个字符,直到到达字符串结尾

,

感谢您的帮助

按照您的建议,我做了类似的事情

    public string Extract(string in_tag)
    {
        string current  = in_tag;
        bool isCandidate = false;
        string tempstring;
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < current.Length; i++)
        {
            char c= current[i];

            if(c== '[')
            {
                isCandidate = true;
            }

            if(isCandidate)
            {
                result.Append(c);
                if(c == ']')
                {
                    tempstring = result.ToString();
                    result.Clear();
                    for (int j = 0; j < _entriesAry.Length; j++)
                    {
                        if(_entriesAry[j].IsMatchTag(tempstring))
                        {
                            current.Replace(tempstring,_entriesAry[j].ReplacedWord());
                        }
                    }
                    isCandidate = false;
                }
            }
        }

        return current;
    }

但是我没有得到预期的结果

本文链接:https://www.f2er.com/3060102.html

大家都在问