4

I have strings that look like "01", "02". Is there an easy way that I can change the string into a number, add 1 and then change it back to a string so that these strings now look like "02", "03" etc. I'm not really good at C# as I just started and I have not had to get values before.

1
  • Do all your numbers have 0's in front or is that just an example? Commented May 14, 2011 at 5:30

5 Answers 5

13

To get from a string to an integer, you can youse int.Parse():

int i = int.Parse("07");

To get back into a string with a specific format you can use string.Format():

strings = string.Format("{0:00}",7);

The latter should give "07" if I understand http://www.csharp-examples.net/string-format-int/ correctly.

Sign up to request clarification or add additional context in comments.

7 Comments

+1 This is correct. The "00" part of the format string indicates that you want 2 or more digits, padded left with 0 if needed to ensure 2 digits.
+1 this with the caveat of a preference for using TryParse instead of straight parse.
Actually, if you are turning only one number into a string, it's slightly more efficient to call the ToString instance method.
@phoog ToString would format it as 7 not as 07 which was requested.
@Anders Abel: not so. There is an overload that takes a string parameter (that is, 7.ToString("00") would return "07"). It requires both less typing and fewer instructions than calling string.Format.
|
0

You can convert the string into a number using Convert.ToInt32(), add 1, and use ToString() to convert it back.

int number = Convert.ToInt32(originalString);
number += 1;
string newString = number.ToString();

Comments

0

Parse the integer

int i = int.Parse("07");

add to your integer

i = i + 1;

make a new string variable and assign it to the string value of that integer

string newstring = i.ToString();

1 Comment

Sorry missed the format part :P Read too fast! See response below :)
0
AddStringAndInt(string strNumber, int intNumber)  
{
    //TODO: Add error handling here
    return string.Format("{0:00}", (int.TryParse(strNumber) + intNumber));
}

1 Comment

This function is garbage in its current form, TryParse takes an out parameter which holds the parsed value. And the return value of the TryParse (which is a boolean) will be what is inserted in the string.Format.
0
static string StringsADD(string s1, string s2)
{
    int l1 = s1.Count();
    int l2 = s2.Count();

    int[] l3 = { l1, l2 };
    int minlength = l3.Min();
    int maxlength = l3.Max();
    int komsu = 0;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < maxlength; i++)
    {

        Int32 e1 = Convert.ToInt32(s1.PadLeft(maxlength, '0').ElementAt(maxlength - 1 - i).ToString());
        Int32 e2 = Convert.ToInt32(s2.PadLeft(maxlength, '0').ElementAt(maxlength - 1 - i).ToString());
        Int32 sum = e1 + e2 + komsu;
        if (sum >= 10)
        {
            sb.Append(sum - 10);
            komsu = 1;
        }
        else
        {
            sb.Append(sum);
            komsu = 0;
        }
        if (i == maxlength - 1 && komsu == 1)
        {
            sb.Append("1");
        }

    }

    return new string(sb.ToString().Reverse().ToArray());
}

I needed to add huge numbers that are 1000 digit. The biggest number type in C# is double and it can only contain up to 39 digits. Here a code sample for adding very huge numbers treating them as strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.