C# 如何检查字符的连续性
我想检查我的字符串中有多少个空格。它不应该超过一个 例如:C# 如何检查字符的连续性,c#,.net,string,C#,.net,String,我想检查我的字符串中有多少个空格。它不应该超过一个 例如: this is ok this is NOT ok thisisok this is NOT ok 您可以测试字符串中是否存在两个连续的空格,因为这也将覆盖任何长度超过2的空格字符串。您可以使用Contains方法执行此操作: string testString = "this is not OK"; if (testString.Contains(" ")) { // Bad } 这将使您了解字符串文字中的连续空
this is ok
this is NOT ok
thisisok
this is NOT ok
您可以测试字符串中是否存在两个连续的空格,因为这也将覆盖任何长度超过2的空格字符串。您可以使用
Contains
方法执行此操作:
string testString = "this is not OK";
if (testString.Contains(" "))
{
// Bad
}
这将使您了解字符串文字中的连续空格的逻辑
static void Main(string[] args)
{
string str = "this is NOT ok";
int index = str.IndexOf(' ');
//check if next character of space is also a space
if (index >= 0 && str[index + 1] == ' ')
Console.WriteLine("Not Ok String");
else
Console.WriteLine("OK String");
}
你说的成功是持续的吗?是的,我想我用词选得不好。我的意思是连续的重复或者连续的也可以看到这个@AlexeiLevenkov你标记为重复的问题不是这个问题的重复。这个问题询问的是连续字符串,而重复的子字符串出现在完整字符串的任何位置。Habib链接到的问题是一个更合适的副本。@JohnKoerner刚刚重新打开,dupelhammer作为Habbib提供的副本-它实际上是完全相同的副本(我提供的副本需要在应用之前进行一些思考,正如我在前面的评论中所添加的)。并且有更具体的代码,然后你的好答案:)由于很有可能关闭一个和超出范围的错误,我不推荐这个代码。。。“你错过了这个和那个”。