本文介绍了如何在C#中添加C001 + 12 = C013的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中添加C001 + 12 = C013

实际上,我不仅要针对C001 + 12 = C013,而且要动态解决方案.
我想要像:

C001 + 12 = C013
C001 + 3 = C004
C009 + 154 = C163

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

推荐答案

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!


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);
}



这将为您提供输出:

字符串是:C,数字是:001

通过她,您可以对数字"执行所有数字运算.

yesotaso
的建议
自定义建立自己的课程



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

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;
  }
}


int c1 = 0xC001;
int c12 = 0x12;
int answer = c1 + c12;
Console.WriteLine("The answer is {0:X}", answer);


这篇关于如何在C#中添加C001 + 12 = C013的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:20