Hi to all, I was wandering if there is build-in method to search in a string, I'll explain my self:
I have the following string(for example)
this- is; text, example for the forum

I need to search for a certain word(lets say example) and look the closer char(from a list of chars '-' ';' ','), from left to right.
1. After searching I have the index of example 11
2. The second index will be 9 because in the index 9, I have the char ','

My Problem How I can search in a string from right to left(the method indexof search from left to rigth)
My second problem How I can search a group of chars, without using indexof a lot of times.
Thanks for help

Your question is a little cryptic. Could you please rephrase it more clearly?
For instance is your example string: this- is; text, example for the forum or is it shorter?

This doesn't make much sense but let me try...

Work the original string backwards:

private void simpleButton1_Click(object sender, EventArgs e)
    {
      const string str = @"this- is; text, example for the forum";
      char[] delims = new char[] { '-', ';', ',' };
      
      //Handle the original string going backwards
      for (int i1 = str.Length - 1; i1 >= 0; i1++)
      {
        char aChar = str[i1];
        if (delims.Contains(aChar))
          Debugger.Break(); //You have a match and an index
      }

      Debugger.Break(); 
    }

Work a reversed string forward:

private void simpleButton5_Click(object sender, EventArgs e)
    {
      const string str = @"this- is; text, example for the forum";
      string reversed = Reverse(str);
      char[] delims = new char[] { '-', ';', ',' };

      //Handle the reversed string going forward
      for (int i1 = 0; i1 < reversed.Length; i1++)
      {
        char aChar = reversed[i1];
        if (delims.Contains(aChar))
          Debugger.Break(); //You have a match and an index
      }

      Debugger.Break();
    }
    public static string Reverse(string x)
    {
      char[] charArray = new char[x.Length];
      int len = x.Length - 1;
      for (int i = 0; i <= len; i++)
        charArray[i] = x[len - i];
      return new string(charArray);
    }

If you're trying to parse the text as something like:
Forum Title - Post Title; Summary of Post

You may want to check out RegEx groups.

This doesn't make much sense but let me try...

Work the original string backwards:

private void simpleButton1_Click(object sender, EventArgs e)
    {
      const string str = @"this- is; text, example for the forum";
      char[] delims = new char[] { '-', ';', ',' };
      
      //Handle the original string going backwards
      for (int i1 = str.Length - 1; i1 >= 0; i1++)
      {
        char aChar = str[i1];
        if (delims.Contains(aChar))
          Debugger.Break(); //You have a match and an index
      }

      Debugger.Break(); 
    }

If you're trying to parse the text as something like:
Forum Title - Post Title; Summary of Post

You may want to check out RegEx groups.

In your simpleButton1_Click method (the first of your two examples) your for loop which starts at str.Length - 1 and ostensibly goes backwards should do i1-- in each loop not i1++ . Otherwise you get an 'array index out of range' error from the compiler

Edited 6 Years Ago by Astro.Coder: HTML was wrong

Comments
Very nice observation!

There is also LastIndexOf() which searches from the end of the string working backwards. You can optionally provide a starting index:

string s = "test.test.test";
            int LastIndex = s.LastIndexOf(".");  // 9

            //search includes character at startIndex so use LastIndex - 1 to avoid matching the same character
            int MiddleIndex = s.LastIndexOf(".", LastIndex -1); // 4

You might also want to look at the IndexOfAny and LastIndexOfAny methods if you are matching more than one character.

This article has been dead for over six months. Start a new discussion instead.