计算字符串 C# 中单词之间的制表符
Count tabs between words in a string C#
我需要计算字符串中单词之间的制表符数量
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 个不同的计数吗?
如果字符串有制表符(而不是表示制表符的 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');
}
Note: No error handling if fromVal
and toVal
is not part of source
.
使用有关如何从 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');
按关键字拆分字符串然后计数。
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');
让我们试试单眼皮!使用正则表达式!
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 指令
我需要计算字符串中单词之间的制表符数量
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 个不同的计数吗?
如果字符串有制表符(而不是表示制表符的 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');
}
Note: No error handling if
fromVal
andtoVal
is not part ofsource
.
使用有关如何从 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');
按关键字拆分字符串然后计数。
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');
让我们试试单眼皮!使用正则表达式!
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
!