none
C# IndexOfAny(array) RRS feed

  • Question

  • Hello guys,

    I am new into programming and there's something I don't understand. When I do IndexOfAny, it gives me 5 as a result which is correct but when it searches again, the result is -1 and it stops the program.

    static void sz1(ref string dat1)
            {
                char[] ArrayZeichen = { '.', ':', ',', ';', '!', '?' };
                int i;
    
                dat1 = dat1.Trim();
                string dat2 = String.Copy(dat1);
                do
                {
                    i = dat2.IndexOfAny(ArrayZeichen);
    
                    Console.WriteLine(i);
                    Console.WriteLine(dat1);
    
                    if (i != -1)
                    {
                        dat1 = dat1.Insert((i + 1), " ");
                        dat1 = dat1.Replace("  ", " ");
                        dat2 = dat2.Remove(i);
                    }
                } while (i != -1);
            }

    Monday, January 8, 2018 4:43 PM

All replies

  • Hi Reoxxx,

    This is where learning to use debugging is extremely helpful. Have a look here.

    The reason for your result is because on the first time around the loop, you find the character you are looking for in dat2. This writes out the position, e.g. 5.

    Further down you then call:

    dat2 = dat2.Remove(i);

    This removes all the characters from dat2 from that position onwards. So if dat2 was originally "ABDCE:ZZZ", then after this call it will be set to "ABCDE".

    You then loop around and call dat2.IndexOfAny again, but you have now removed those characters, so the result is -1!


    • Edited by RJP1973 Monday, January 8, 2018 5:02 PM
    • Marked as answer by Reoxxx Monday, January 8, 2018 5:46 PM
    • Unmarked as answer by Reoxxx Monday, January 8, 2018 8:34 PM
    Monday, January 8, 2018 5:01 PM
  • Ok thank you for that. I fixed this part but there's now another problem and I don't know how to start with it. After all these characters, I put a spacebar. Now I have to remove spacebars before the characters, I tried to do it with String.Remove/Replace but with no success.

    This is the program:

    static void sz1(ref string dat1)
            {
                char[] ArrayZeichen = { '.', ':', ',', ';', '!', '?' };
                int i = 0;
    
                dat1 = dat1.Trim();
                for (int k = 0; k < dat1.Length; k++)
                {
                    dat1 = dat1.Replace("  ", " ");
                }
    
                do
                {
                    i = dat1.IndexOfAny(ArrayZeichen, i+1);
    
                    if (i != -1)
                    {
                        dat1 = dat1.Insert((i + 1), " ");
                        dat1 = dat1.Replace("  ", " ");
                    }
                } while (i != -1);
            }

    Monday, January 8, 2018 8:20 PM
  • Hello Reoxxx,

    >> Now I have to remove spacebars before the characters, I tried to do it with String.Remove/Replace but with no success.

    Did you consider using the String.Replace method directly?

     static void sz1(ref string dat1)
            {
               dat1= dat1.Replace(" ", "");          
            }

    Or using Linq

     static void sz1(ref string dat1)
            {
               dat1=String.Join("",dat1.Where(x => x != ' '));     
            }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 9, 2018 2:17 AM
    Moderator
  • Your statement of the problem is a bit confusing.  Is the goal to insert one space after punctuation characters, but only if there is not already a space there?

    FYI, the string.Replace method replaces ALL occurrences of the search string.  Thus, your first loop is silly.  You're replacing every occurrence of two spaces with one space, but you're doing it over and over and over.

    You might consider implementing your loop just as I put it into words above:

        while( (i = dat1.IndexOfAny(ArrayZeichen, i+1)) >= 0 )
        {
            if( dat[i+1] != ' ' )
                dat1 = dat1.Insert(i + 1, " ")
        }
    There's actually a bug in that snippet, which would be encountered if there was punctuation as the very last character in the string.  I'll leave it to you to fix that.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, January 9, 2018 7:00 PM
  • Hello Reoxxx,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 12:56 PM
    Moderator