如何在C#中逐字逐句迭代?

Par*_*tha 4 c#

我想逐字逐句地遍历字符串.

如果我有一个字符串"incidentno和fintype或unitno",我想逐一阅读每个单词"incidentno","and","fintype","or"和"unitno".

Guf*_*ffa 16

foreach (string word in "incidentno and fintype or unitno".Split(' ')) {
   ...
}
Run Code Online (Sandbox Code Playgroud)

  • @synhershko:不,它只会分裂一次. (3认同)

Dar*_*rov 13

var regex = new Regex(@"\b[\s,\.-:;]*");
var phrase = "incidentno and fintype or unitno";
var words = regex.Split(phrase).Where(x => !string.IsNullOrEmpty(x));
Run Code Online (Sandbox Code Playgroud)

即使.,; tabs and new lines你的单词之间有" ",这也有效.

  • 没有这样的方法.我使用的是Regex.Split而不是String.Split (2认同)

JDu*_*ley 11

稍微扭曲我知道,但您可以将迭代器块定义为字符串上的扩展方法.例如

    /// <summary>
    /// Sweep over text
    /// </summary>
    /// <param name="Text"></param>
    /// <returns></returns>
    public static IEnumerable<string> WordList(this string Text)
    {
        int cIndex = 0;
        int nIndex;
        while ((nIndex = Text.IndexOf(' ', cIndex + 1)) != -1)
        {
            int sIndex = (cIndex == 0 ? 0 : cIndex + 1);
            yield return Text.Substring(sIndex, nIndex - sIndex);
            cIndex = nIndex;
        }
        yield return Text.Substring(cIndex + 1);
    }

        foreach (string word in "incidentno and fintype or unitno".WordList())
            System.Console.WriteLine("'" + word + "'");
Run Code Online (Sandbox Code Playgroud)

其优点是不为长字符串创建大数组.

  • 我喜欢这个替代方案,对于大量数据非常有用,你真的应该获得+1! (2认同)

Mik*_*per 5

使用字符串类的Split方法

string[] words = "incidentno and fintype or unitno".Split(" ");
Run Code Online (Sandbox Code Playgroud)

这将在空格上拆分,因此“单词”将具有[incidentno,and,fintype,or,unitno].