Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i add C001+12 = C013 in C#

Actually i want dynamic solution, not only for C001+12 = C013.
i want like:

C001+12=C013
C001+3=C004
C009+154=C163
Posted
Updated 27-May-11 22:55pm
v2
Comments
Richard MacCutchan 28-May-11 11:41am    
This question is really not very clear. You have a number of different suggestions, one or more of which may be the solution. Please add some more detail about what problem you are trying to solve.

You can't, not directly.
You need to break the string "C001" into it's two parts: string and integer.
If this is a fixed format, it's easy:
string inText = "C001";
int inVal = 12;
string text = inText.Substring(0, 1);
string number = inText.Substring(1);
int val = int.Parse(number);
val += inVal;
string outText = string.Format("{0}{1,3:D3}", text, val);
If it isn't fixed, then it gets a little harder - but not much!
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-May-11 11:48am    
I really like Kim's solution better, but since OP didn't mention any requistes like format, pattern etc. take a 5+ :)
You can use regular expression to separate string and number.

Regex string to use: (?<string>[A-Za-z]+)(?<number>\d+)

C#
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match("C001", @"(?<string>[A-Za-z]+)(?<number>\d+)");

if (m.Success)
{
  Console.WriteLine("String is: " + m.Groups["string"].Value + ", Number is: " + m.Groups["number"].Value);
}


This will give you an output:

String is: C, Number is: 001

From her you can do all numeric operations on "number".

Suggestion from yesotaso

Custom build your own class
C#
class myInt
{
  public int Value;
  public static myInt operator +(myInt left,int right)
  {
    return new myInt() {Value=left.Value + right};
  }

  public override string ToString()
  {
    return string.Format("C{0,3:D3}",Value); //OriginalGriff's code
  } 

  public bool FromString(string input)
  {
    System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, @"(?<string>[A-Za-z]+)(?<number>\d+)"); // Kim's code

    if (m.Success)
      this.Value = m.Groups["number"].Value;

    return m.Success;
  }
}
 
Share this answer
 
v4
Comments
yesotaso 28-May-11 7:34am    
Good approach, and if I may add
class myInt{
public int Value;
public static myInt operator +(myInt left,int right)
{return new myInt(){Value=left.Value + right};}
public override string ToString() {return string.Format("C{0,3:D3}",Value);} //OriginalGriff's code
public bool FromString(string input){
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, @"(?<string>[A-Za-z]+)(?<number>\d+)"); // Kim's code
if (m.Success) this.Value = m.Groups["number"].Value;
return m.Success;
}
}
etc :)
Kim Togo 28-May-11 9:57am    
Thanks, and yes good code :-)
Kim Togo 28-May-11 10:18am    
Taken your idea and improved my answer. Credit goes to you yesotaso.
Kim Togo 28-May-11 14:08pm    
@yesotaso, You can try add a solution your self :-)
yesotaso 28-May-11 15:20pm    
Nah, I am content as it is :) 1 thing makes me veery curious though :D
Quote: From her you can do all numeric operations on "number"
int c1 = 0xC001;
int c12 = 0x12;
int answer = c1 + c12;
Console.WriteLine("The answer is {0:X}", answer);
 
Share this answer
 
Comments
Manfred Rudolf Bihy 28-May-11 11:46am    
Thanks Richard, that one did make me chuckle quite a bit! 5+
Richard MacCutchan 28-May-11 12:40pm    
Well, I'm still not sure what the question is!
Manfred Rudolf Bihy 28-May-11 14:50pm    
:-)
C#
int num;
num = 12;
String str= String.Format("C{0:000}", num);
 
Share this answer
 
v2
Well, if you take any type here. It will work according to their predefined logics and syntaxes.

Well, I would have suggested "dynamic" type

Ex:

dynamic b = "C001" + "12"

Will become C00112

So I think you should use string functions for this.

Such as Substring except first character you can go with remainingssss

001 + 12 = 013 with pading of zeros.

:)

Thanks :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900