tags:

views:

1582

answers:

10

If I have a string (010) and i want to add 1 to it (011) what value type should i use to convert this string into a number for adding and at the same time preserve the whole number and not 10 + 1 = 11.

+5  A: 
if (int.TryParse(str, out i))
    str = (i + 1).ToString("000");

HTH.

(edit: fixed the problems pointed out by BoltBait and steffenj)

Omer van Kloeten
You'll lose the leading zero.
BoltBait
You can fix that by using a format string in the ToString() method. I think .ToString("G:000###") or something like that.
steffenj
A: 

Would it be possible to just always ensure that there are three digits?

So, you would do the math as usual, but create a function to attach extra zeros to the front, if needed?

UnhipGlint
+13  A: 

You could use something like this:

string initialValue = "010";
int tempValue = Int.Parse(initialValue) + 1;
string newValue = tempValue.ToString("000");

You do your math as normal and then just return your string to its previous format using the number formatting feature of the .ToString()

Ilya Kochetov
Nice job. More info here: http://www.csharp-examples.net/string-format-int/
BoltBait
Thank. That work perfect!
Jack
A: 

Well, you could always create your own struct which contained the int and required output length (based on the input length). Or just remember it very temporarily, as shown below... it depends on how often you need this.

string ParseAndAdd(string text, int add)
{
    int parsed = int.Parse(text);
    return (parsed+add).ToString().PadLeft(text.Length, '0');
}
Jon Skeet
A: 
int value = 10;// or, int value = Convert.ToInt32("010");
value += 1;
string text = value.ToString("000");

The "000" in the ToString call is called a format string. It tells .net how to print out the number. In this case, the character '0' indicates that for the number at this position, it should display a zero if the number would not otherwise be displayed.

David Thibault
+1  A: 

Try something like this:

string a = "010";
string b = "1";
a = (int.Parse(a) + int.Parse(b)).ToString(new string('0', Math.Max(a.Length, b.Length)));
Console.WriteLine(a);

This allows to cope for arbitrary lengths of the a and b strings.

smink
A: 

It looks like Binary operation on the string, I think you should create a BinaryString class which should have internal Binary to decimal conversion and then overload +,- etc and do the decimal operation inside. and output the result as binary string.

Jobi Joy
+3  A: 

It looks like you're trying to work with binary numbers encoded as strings (hmmm. maybe there's a place for clippy in visual studio). You can use Convert() methods to do this for you. The 2 is used to indicate base-2 formatting. If you need the string to be a certain size, you may have to add zero padding.

string s = "010";
s = Convert.ToString(Convert.ToInt32("010", 2) + 1, 2);
Ferruccio
A: 
string str = "110";
int i = 0;
int maxSize = 3;
if (int.TryParse(str, out i))
{
    str = string.Concat(new string('0', maxSize - (i + 1).ToString().Length), i + 1);
}
Vyas Bharghava
Thanks Ferruccio... Newbie blues... :)
Vyas Bharghava
+1  A: 

Here's how I'd do it:

    public string AddOne (string text)
 {
  int parsed = int.Parse(text);
  string formatString = "{0:D" + text.Length + "}";
  return string.Format(formatString, parsed + 1);
 }

By putting the length of the input text into the format string, you can ensure that your resulting string is the same length as your input.

Depending on your needs, you may need exception handling around the int.Parse. I thought I'd let the exception bubble up as the exceptions thrown (ArgumentException or ArgumentNullException) by int.Parse would be the same exceptions that I would throw in my method anyway.