Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3943454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:47:02+00:00 2026-05-20T00:47:02+00:00

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

  • 0

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="";

    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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:47:03+00:00Added an answer on May 20, 2026 at 12:47 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.