Dragan
Dragan

Reputation: 3743

Substring difficulty C#

I have the following method:

public static string PrepareNumberForInserting(string pNumber)
        {
            if (pNumber.Length > 7)
            {
                if (pNumber.Length == 8 && pNumber.Substring(0, 1) == "7")
                {
                    pNumber = pNumber.Substring(1, 8);
                }

                if (pNumber.Length == 9 && pNumber.Substring(0, 2) == "07")
                {
                    pNumber = pNumber.Substring(2, 9);
                }

                if (pNumber.Length == 11 && pNumber.Substring(0, 4) == "3897")
                {
                    pNumber = pNumber.Substring(4, 11);
                }

                if (pNumber.Length == 12 && pNumber.Substring(0, 5) == "38907")
                {
                    pNumber = pNumber.Substring(5, 12);
                }
            }
            else
            {
                pNumber = string.Format("3897{0}", pNumber);
            }

            return pNumber;
        }

regardless of what format the user enters his number (be it 070300067, 70300067, xxx70300067), i want to extract the last 7 characters and prefix them with 3897. If I enter anything other than 7xxxxxx i get a Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.

Any idea? Thank you very much!

Edit:

I solved my problem in the following way:

public static string PrepareNumberForInserting(string pNumber)
        {
            if (pNumber.Length > 7)
            {
                if (pNumber.Length == 8 && pNumber.StartsWith("7"))
                {
                    pNumber = pNumber.Substring(1);
                }

                if (pNumber.Length == 9 && pNumber.StartsWith("07"))
                {
                    pNumber = pNumber.Substring(2);
                }

                if (pNumber.Length == 11 && pNumber.StartsWith("3897"))
                {
                    pNumber = pNumber.Substring(4);
                }

                if (pNumber.Length == 12 && pNumber.StartsWith("38907"))
                {
                    pNumber = pNumber.Substring(5);
                }
            }

                pNumber = string.Format("3897{0}", pNumber);


            return pNumber;
        }

Thank you all for taking the time to answer my question!

Upvotes: 1

Views: 1164

Answers (6)

Salvatore Previti
Salvatore Previti

Reputation: 9080

public static string PrepareNumberForInserting(string pNumber)
{
    int idx = pNumber.IndexOf('7');
    return "3897" + pNumber.SubString(idx >= 0 ? idx + 1 : 0);
}

Upvotes: 1

Dan
Dan

Reputation: 124

In the statements where you're skipping the first x characters and taking the substring, you're grabbing one too many characters. In other words

pNumber = pNumber.Substring(2, 9);

should be

pNumber = pNumber.Substring(2, 8);

On the other hand, if you are always wanting to grab the last 7 characters then you could just do something this this:

      if (pNumber.Length > 7)
        {
            pNumber = string.Format("3897{0}", pNumber.Substring(pNumber.Length - 7, 7));
        }
        else
        {
            pNumber = string.Format("3897{0}", pNumber);
        }

Upvotes: 1

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

The substring method takes a start index and a length.

But as an alternate try the following:

var phoneNumber = "3897" + pNumber.Substring(Math.Max(pNumber.Length - 7, 0));

Upvotes: 1

CodesInChaos
CodesInChaos

Reputation: 108880

Look at the documentation for Substring:

public string Substring(int startIndex, int length)

The second parameter to substring is a length, not the end index. Thus code like:

pNumber = pNumber.Substring(5, 12);

Will take characters 5 to 16, and not 5 to 11 as you expected. Since your string has only 12 characters this leads to the ArgumentOutOfRangeException you observed.

To fix the problem, you can either calculate the length (length=endIndex-startIndex+1), or if you need everything after a certain index you can simply use the other overload:

pNumber = pNumber.Substring(5);

Upvotes: 5

iandayman
iandayman

Reputation: 4467

               if (pNumber.Length == 8 && pNumber.Substring(0, 1) == "7")
               {
                   pNumber = pNumber.Substring(1, 8);
               }

That's saying that if the length = 8 then take 8 characters after character 1 (i.e. to 9 which is one more than the length)

Upvotes: 1

drdwilcox
drdwilcox

Reputation: 3951

SubString takes a location and length, not two locations.

Upvotes: 3

Related Questions