简体   繁体   English

Surefire在C#中获取字符串字数统计的方法是什么

[英]What is a Surefire way to get a string Word Count in C#

I am not sure how to go about this. 我不确定该怎么做。 Right now I am counting the spaces to get the word count of my string but if there is a double space the word count will be inaccurate. 现在,我正在计算空格以获取字符串的字数,但是如果存在双倍空格,则字数将不准确。 Is there a better way to do this? 有一个更好的方法吗?

Alternate Version of @Martin v. Löwis, which uses a foreach and char.IsWhiteSpace() which should be more correct when dealing with other cultures. @Martin v。Löwis的替代版本,它使用foreachchar.IsWhiteSpace() ,在处理其他文化时应该更正确。

int CountWithForeach(string para)
{
    bool inWord = false;
    int words = 0;
    foreach (char c in para)
    {
        if (char.IsWhiteSpace(c))
        {
            if( inWord )
                words++;
            inWord = false;
            continue;
        }
        inWord = true;
    }
    if( inWord )
        words++;

    return words;
}

While solutions based on Split are short to write, they might get expensive, as all the string objects need to be created and then thrown away. 尽管基于Split的解决方案很难编写,但它们可能会变得昂贵,因为所有的字符串对象都需要创建然后丢弃。 I would expect that an explicit algorithm such as 我希望有一个明确的算法,例如

  static int CountWords(string s)
  {
    int words = 0;
    bool inword = false;
    for(int i=0; i < s.Length; i++) {
      switch(s[i]) {
      case ' ':case '\t':case '\r':case '\n':
          if(inword)words++;
          inword = false;
          break;
      default:
          inword = true;
          break;
      }
    }
    if(inword)words++;
    return words;
  }

is more efficient (plus it can also consider additional whitespace characters). 效率更高(此外,它还可以考虑其他空格字符)。

This seems to work for me: 这似乎为我工作:

var input = "This is a  test";
var count = input.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length;

Try string.Split : 尝试string.Split

string sentence = "This     is a sentence     with  some spaces.";
string[] words = sentence.Split(new char[] { ' ' },  StringSplitOptions.RemoveEmptyEntries);
int wordCount = words.Length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中,建议使用什么方法来确定字符串是否为真实单词? - In C#, what is the recommended way to establish whether or not a string is a real word? 最好的方法是在C#中的字符串中获取第一个单词和其余单词 - Best way get first word and rest of the words in a string in C# 在C#中获取HttpPostedFile的字符串内容的最短方法是什么? - What is the shortest way to get the string content of a HttpPostedFile in C# 不使用C#中的for循环来获取单词表的单元格索引的最佳方法是什么? - What is the best way to get cell index of a word table without using for loop in c#? 将 JavaScript 代码转换为 C# 以获得频繁字数 - Convert JavaScript code to C# to get Frequent Word Count 用C#从id集合中获取4个(或任意数量)随机唯一ID的最简单方法是什么? - What is the easiest way to get 4(or any count) random unique ids from an id collection with c#? 使用 mvvm 和 c# 获取选定行数的正确方法是什么? - What is the correct way to get the selected rows count using mvvm and c#? C#中的字数统计算法 - Word Count Algorithm in C# 计算C#中另一个字符串中出现的字符串的最快方法 - The fastest way to count string occurences in another string in c# 有没有更好的方法来计算C#中字符串中的字符串格式占位符? - Is there a better way to count string format placeholders in a string in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM