开发者

Need help with . string.substring(0, max.Length)

ArgumentOutofBounds Exceptions is thrown all the times inside the ifs in the loop.

In this code I am trying to send two strings between the symbol @2@3.

string1+"@2@3"+string2

Now I try to separate the strings from the symbols by the method substring, but an exception is being thrown over there......

public void seperatePMChattersNames(string TwoNames)
{
    string nameOne="";
    string nameTwo="";
开发者_JAVA百科
    Console.WriteLine(TwoNames);
    for (int i = 0; i < TwoNames.Length; i++)
    {
        if (TwoNames[i] == '2' && TwoNames[i-1] == '@')///ArgumentOutofRange Exception
        {
                nameOne = TwoNames.Substring(0, i);
        }
        if (TwoNames[i] == '@' && TwoNames[i+1] == '3')///ArgumentOutofRange Exception
        {           
            nameTwo = TwoNames.Substring(i+1, TwoNames.Length);               
        }
    }
}

Why is it thrown and how to prevent it?


When i is zero, TwoNames[i - 1] will try to access index -1 of the string - that doesn't exist.

When i is TwoNames.Length - 1, TwoNames[i + 1] will try to access past the end of the string.

Next, when you have found "@3", you're using:

TwoNames.Substring(i+1, TwoNames.Length)

the second parameter of Substring is the length of the substring to take, not the final index. If you just want the rest of the string, you can omit the second argument:

TwoNames.Substring(i+1)

Note that that would include the "3" though - so you probably really want i+2 instead of i+1.

Is there any reason you're not using string.IndexOf(TwoNames, "@2") etc?

If you just want nameOne to be the string before the first "@2" and nameTwo to be the string after the last "@3", you can use:

int endOfOne = TwoNames.IndexOf("@2");
if (endOfOne != -1)
{
    nameOne = TwoNames.Substring(0, endOfOne);
}
else
{
    // Couldn't find @2... throw exception perhaps?
}

int startOfTwo = TwoNames.LastIndexOf("@3");
if (startOfTwo != -1)
{
    // Allow for the "@3" itself
    nameTwo = TwoNames.Substring(startOfTwo + 2);
}
else
{
    // Couldn't find @3... throw exception perhaps?
}

Another option is to use regular expressions, of course - they add a degree of complexity themselves, but they really are aimed at pattern matching. If you find you need to get even more bits and pieces, they could help.


Your loop starts with i = 0 but in your first if-statement you try access TwoNames[i-1] which tries to access TwoNames[-1]. Clearly this will cause an issue since there is no TwoName[-1].

Your loop should start with i = 1, not 0.


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

should be

 for (int i = 1; i < TwoNames.Length-1; i++)


This is the answer

public void seperatePMChattersNames(string TwoNames)
        {
                Console.WriteLine(TwoNames);
                int x = TwoNames.IndexOf("@2");
                int y = TwoNames.IndexOf("@3");
                string nameone = TwoNames.Substring(0, x);
                string nametwo = TwoNames.Substring(y+2);
                Console.WriteLine(nameone);
                Console.WriteLine(nametwo);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜