string

i have a string called str and a char array called dizi.

i have an if statement.if the string hasn't one of array members the if statement must be run.but it isnt working of course.where's my mistake :(

if (!str[j].ToString().IndexOfAny(dizi))

{

gecici = str[j] + gecici;

index[l] = j;

l++;

}



Answer this question

string

  • Coroebus

    To check if a string has any of the characters in a char array do this:

    string s = "abcdefghijklm";
    char[] chars = { 'a', 'q', 'z' };

    if (s.IndexOfAny(chars) != -1)
    {
    // true
    }

    Cheers


  • Kailai

    if (!str[j].ToString().IndexOfAny(dizi)) // Won't compile because IndexOfAny() returns an int, specifying the index of character found in the string and returns -1 if none of char is found else it returns the found index...

    So your code willl be:

    if (!str[j].ToString().IndexOfAny(dizi) != -1) // It will compile fine

    Best Regards,

    Rizwan



  • Nick K.

    hi, merwy

    If you want to pick out the char exists in the str but not in dizi, you can do like this:

                string gecici = string.Empty;

                for (int i = 0; i < str.Length; i++)

                {

                    if (str [ i ].ToString().IndexOfAny(dizi) == -1)

                        gecici += str [ i ];

                }

    BR



  • Sam Dela Cruz

    First of all,

    if (!str[j].ToString().IndexOfAny(dizi) != -1)

    is the same as

    if (str[j].ToString().IndexOfAny(dizi) == -1)

    which is a bit easier to understand. However, assuming str is a string, str[j] is a character and dizi is an array of characters, you'd be much better off convert dizi to a string, and reversing the search:


    string strDizi = new string(dizi);
    for (int j = 0; j < str.Length; ++j)
    if (strDizi.IndexOf(str[j]) == -1)



  • Joost Schermers

     merwy wrote:

    i have an if statement.if the string hasn't one of array members the if statement must be run.but it isnt working of course.where's my mistake :(

    if (!str[j].ToString().IndexOfAny(dizi))

    {

    gecici = str[j] + gecici;

    index[l] = j;

    l++;

    }

    It make me confused that you want to collect the char (and its position in str) which is in the dizi but not in the str or the reversed way

    It seems that you want to pick out the char exists in the str but not in dizi...



  • string