简体   繁体   English

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

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

I need help to check if a string contains a word like: PIETSNOT .我需要帮助来检查字符串是否包含这样的单词: PIETSNOT 。 COM通讯

I don't want it to say true when I have it like this: "Hi my name is" etc., only if it is 1 letter space 1 letter and that 3 times or more in a string.当我有这样的时候,我不希望它说真的:“嗨,我的名字是”等等,只有当它是 1 个字母空格 1 个字母并且在一个字符串中出现 3 次或更多时。 This is for my game's banfilter.这是我的游戏的banfilter。 I dont want that peaple can say Hi come to my game HIHIHI .我不希望那个人可以说嗨来参加我的游戏 HIHIHI。 COM通讯

I can't figure it out because I don't want to add all the combinations for 1 word.我想不通,因为我不想为 1 个单词添加所有组合。

Strictly following your requirement, everything that is not a letter followed by a space and repeat the sequence until the end of the string严格按照您的要求,所有不是字母后跟空格的内容并重复序列直到字符串结束

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;
}

This should do the trick:这应该可以解决问题:

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);

A Regular expression solution could look like this:正则表达式解决方案可能如下所示:

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");

If you need it to match more letters than capital letters and dot, you should extend the two [AZ.] to eg [A-Za-z0-9.,-_].如果您需要它匹配比大写字母和点更多的字母,您应该将两个 [AZ.] 扩展为例如 [A-Za-z0-9.,-_]。

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

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