简体   繁体   English

检查字符串中的字符包含

[英]Check character containment in a string

I am doing a homework question to split a string. 我正在做一个作业问题,以分割字符串。 I have a doubt in that. 我对此表示怀疑。

string text = "my text";
char[] delim = new char[]{' '};

How can I check if text[i] is in delim , without having code iteration? 如何在没有代码迭代的情况下检查text[i]是否为delim

if(text[i] in delim) //not correct
{
}

string has a member named Contains . string具有一个名为Contains的成员。 Is this what you are looking for? 这是你想要的?

Here is the link to the MSDN http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx 这是MSDN的链接http://msdn.microsoft.com/zh-cn/library/dy85x1sa.aspx

you need to do something like this: 您需要执行以下操作:

        string text = "my text";
        string[] txtSplit = null;
        if (text.Contains(" "))
        {
            txtSplit = text.Split(' ');
        }

Please Mark as answered if this helps. 如果有帮助,请标记为已回答。

If I understand your question correctly, you want the different words in a string. 如果我正确理解了您的问题,则需要在字符串中输入不同的单词。 In that case I would recommend to use the Split-method. 在那种情况下,我建议使用拆分方法。

MSDN: http://msdn.microsoft.com/en-us/library/system.string.split.aspx MSDN: http : //msdn.microsoft.com/en-us/library/system.string.split.aspx

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

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