【问题标题】:Count tabs between words in a string C#计算字符串中单词之间的制表符C#
【发布时间】:2018-01-10 07:12:31
【问题描述】:

我需要计算字符串中单词之间的制表符数

string s = "DATE\t\t\tVERSION\t\tAUTHOR\t\t\t\t\tCOMMENTS";


int TabsDateVersion;     //number of tabs between "Date" and "Version"
int TabsVersionAuthor;   //number of tabs between "Version" and "Author"
int tabsAuthorComments;  //number of tabs between "Author" and "Comments"

注意:字词由一个或多个标签分隔。

知道如何在 C# 中执行此操作,所以我有 3 个不同的计数吗?

【问题讨论】:

  • 我能问一下为什么你需要计算标签吗? - 例如,如果您想要一个文本文件,其中单词的格式类似于表格,还有更简单的解决方案
  • 它们实际上是制表符 (\t) 还是像您的示例中那样的空格字符?
  • @IanH。它们只是单词之间的制表符
  • 你知道Split这个功能怎么用吗?您只需要计算返回数组的每个位置的字符数。但是你真的应该考虑发布至少一个自己的尝试来解决这个问题。您是否尝试过至少使用 for 循环?
  • @Raizzen。它来自一个 XML 文件,我坚持使用它

标签: c# delimiter


【解决方案1】:

按关键字拆分字符串然后计数。

 string s = "DATE\t\t\tVERSION\t\tAUTHOR\t\t\tCOMMENTS";
 string[] list = s.Split(new string[] { "DATE", "VERSION", "COMMENTS", "AUTHOR" }, StringSplitOptions.None);
 int TabsDateVersion = list[1].Count(x => x == '\t');     
 int TabsVersionAuthor = list[2].Count(x => x == '\t');
 int tabsAuthorComments = list[3].Count(x => x == '\t');

【讨论】:

    【解决方案2】:

    使用有关如何从 this 答案中获取单词之间文本的信息,我编写了一个简单的函数,该函数获取 string、左侧单词、右侧单词和 char 的总数。

    public int CountCharactersBetweenWords(string totalString, string leftWord, string rightWord, char search)
    {
        // Find the indexes of the end of the left and the start of the right word
        int pFrom = totalString.IndexOf(leftWord) + leftWord.Length;
        int pTo = totalString.LastIndexOf(rightWord);
    
        // Get the substring between the words
        string between = totalString.Substring(pFrom, pTo - pFrom);
    
        // Count the characters that are equal with the character to search for
        return between.Count(c => c == search);
    }
    

    这样称呼它:

    string s = "DATE\t\t\tVERSION\t\tAUTHOR\t\t\t\t\tCOMMENTS";
    int count = CountCharactersBetweenWords(s, "DATE", "VERSION", '\t');
    

    【讨论】:

      【解决方案3】:

      如果字符串有制表符(而不是代表制表符的 4 个空格),您可以使用 LINQ 轻松计算

      var s = "DATE,      VERSION             AUTHOR      COMMENTS";
      
      Console.WriteLine(CountTabs(s,"DATE","VERSION"));
      Console.WriteLine(CountTabs(s,"VERSION","AUTHOR"));
      Console.WriteLine(CountTabs(s,"AUTHOR","COMMENTS"));
      
      public static int CountTabs(string source, string fromVal, string toVal)
      {
          int startIdx = source.IndexOf(fromVal);
          int endIdx = source.IndexOf(toVal);
      
          return source.Skip(startIdx).Take(endIdx - startIdx).Count(c => c == '\t');
      }
      

      注意:如果 fromValtoVal 不是 source 的一部分,则不会进行错误处理。

      【讨论】:

      • 之后如何区分哪些标签属于哪个单词之间的间隙?
      • @Sayse 你是对的,这只给出了标签的总数
      • @BertAR 我正在改变这个
      • @Sayse 已更新。
      【解决方案4】:

      让我们试试一个班轮!使用正则表达式!

      Regex.Match("DATE\t\t\tVERSION\t\tAUTHOR\t\t\t\t\tCOMMENTS", 
          "DATE(\\t*)").Groups[1].Length
      

      这里使用的正则表达式是

      DATE(\t*)
      

      它基本上是查找"DATE" 并捕获其后的所有制表符并将它们全部放在第1组中。您可以将"DATE"替换为"VERSION""COMMENTS"等。

      记得给System.Text.RegularExpressions添加一个using指令!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-31
        • 2020-08-08
        • 2012-09-23
        • 2018-11-07
        • 2023-03-21
        相关资源
        最近更新 更多