locked
Index out of Range Exception RRS feed

  • Question

  • I am searching for a prticular character in a string using IndexOf() method. But i am getting a Index out of range exception . Can anyone tell me why this exception is coming . Please help me out .

    Thanks in advance

    Thursday, January 4, 2007 12:17 PM

Answers

  • Hi

    this is because you are passing in an index which is not in the string

    so example

    string s ="word";

    by index

    0 = w
    1 = o
    2 = r
    3 = d

    if for example you try to pass in index 4 you will get the index out of range exception :)

    Hope this helps you out

    Thursday, January 4, 2007 6:52 PM

All replies

  • Show us some code?
    Thursday, January 4, 2007 1:09 PM
  • Hi

    this is because you are passing in an index which is not in the string

    so example

    string s ="word";

    by index

    0 = w
    1 = o
    2 = r
    3 = d

    if for example you try to pass in index 4 you will get the index out of range exception :)

    Hope this helps you out

    Thursday, January 4, 2007 6:52 PM
  • a common error is that your for loop is running 1 time too many...

    for (int i = 0; i < MyString.Length; i++) // good

    if you run the loop to <= MyString.Lenth, you will have gone 1 character too far..

    Thursday, January 4, 2007 9:06 PM