繁体   English   中英

我如何检查一个字符串包含一个字母空格字母空格和另一个字母 C#

[英]How can i check of a string contains a letter space letter space and anotherl letter C#

我需要帮助来检查字符串是否包含这样的单词: PIETSNOT 。 通讯

当我有这样的时候,我不希望它说真的:“嗨,我的名字是”等等,只有当它是 1 个字母空格 1 个字母并且在一个字符串中出现 3 次或更多时。 这是我的游戏的banfilter。 我不希望那个人可以说嗨来参加我的游戏 HIHIHI。 通讯

我想不通,因为我不想为 1 个单词添加所有组合。

严格按照您的要求,所有不是字母后跟空格的内容并重复序列直到字符串结束

void Main()
{
    string test = "P I E T S N O T . C O M";
    Console.WriteLine(CheckSpaceLetterSequence(test));
    test = "Hi my name is";
    Console.WriteLine(CheckSpaceLetterSequence(test));
}


bool CheckSpaceLetterSequence(string test)
{
    int count = 0;
    bool isSpace = (test[0] == ' ');
    for(int x = 1; x < test.ToCharArray().Length; x++)
    {
        bool curSpace = (test[x] == ' ');
        if(curSpace == isSpace)
            return false;

        isSpace = !isSpace;
        count++;
        if(count == 3)
           break;
    }
    return true;
}

这应该可以解决问题:

bool allLetterSpace = text.Trim().Length >= 6 && text.Trim()
   .Select((c, i) => new { IsWhiteSpace= Char.IsWhiteSpace(c), Index = i })
   .All(x => x.Index % 2 == 1 ? x.IsWhiteSpace : !x.IsWhiteSpace);
string word = "Hello, my name is f i s h b i s c u i t s";

if (word.Replace(" ", "").Contains("fishbiscuits"))
{
 // code here
}

这将告诉您text中的所有其他字符是否都是空格:

bool spaced = text.Select((c,i) => (c == ' ') == (i % 2 == 1)).All(b => b);

正则表达式解决方案可能如下所示:

Regex regex = new Regex(@"(?<name>(?:[A-Z.] ){2,}[A-Z.])");
Match m = regex.Match("P I E T S N O T . C O M");
if (m.Success)
    MessageDlg.Show("Name is " + m.Groups["name"].Value);
else
    MessageDlg.Show("No name found");

如果您需要它匹配比大写字母和点更多的字母,您应该将两个 [AZ.] 扩展为例如 [A-Za-z0-9.,-_]。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM