简体   繁体   中英

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?

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

string has a member named Contains . Is this what you are looking for?

Here is the link to the MSDN http://msdn.microsoft.com/en-us/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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