例如,123--124    a123--a124    aaa--aaa   12a3--12a4   00001--00002  a00001--a00002

解决方案 »

  1.   


    例如a00002 如果把00002转换成Int ,就变成2了吧?!
      

  2.   

      string a = "12354b";
                try
                {
                    string b = a.Substring(a.Length - 1);
                    int c=int.Parse(b)+1;
                    string newA = a.Substring(0, a.Length - 1)+c+"";
                    MessageBox.Show(newA);            }catch{
                
                }
      

  3.   


                List<string> list = new List<string> { "123", "a123", "aaa", "12a3", "00001", "a00001", "12a9" };
                foreach (string s in list)
                {
                    string result = Regex.Replace(s, @"\d$", m => { return (Convert.ToInt32(m.Value) + 1).ToString(); });
                    Console.WriteLine(result);
                }
                Console.ReadLine();
    /*
    124
    a124
    aaa
    12a4
    00002
    a00002
    12a10
    */
      

  4.   


    正在写,就已经有人贴出来了。
    需要改进一下:将string result = Regex.Replace(s, @"\d$"....这里的正则匹配模板改为"\d+$"
      

  5.   


                string str = "abcd123";
                int num =Convert.ToInt32(str.Substring(str.Length - 1));
                //自己加个if判断下是否为数字,是数字的,再转换
      

  6.   

    也还不对:
    如果简单地用“\d$","12a99"得到的就是"12a910",应该是"12a100"
    如果换成"\d+$", "0001"得到的就是"2"了,0被丢失了要继续完善正则匹配模板
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] array = { "123", "a123", "aaa", "12a3", "12a4", "00001", "a00001", "12a9", "12a99" };
                array.ToList().ForEach(x => Console.WriteLine("{0}-{1}", x, AddOne(x)));
            }        static string AddOne(string source)
            {
                Regex regex = new Regex(@"(^\w+?|^\d+\w+?)(\d+$)");
                Match match = regex.Match(source);
                return match.Groups[1].Value == "" ? 
                        source : 
                        match.Groups[1].Value 
                    + (match.Groups[2].Value == "" ? 
                        "" : 
                        (Convert.ToInt32(match.Groups[2].Value) + 1).ToString().PadLeft(match.Groups[2].Length, '0'));
            }
        }
    }123-124
    a123-a124
    aaa-aaa
    12a3-12a4
    12a4-12a5
    00001-00002
    a00001-a00002
    12a9-12a10
    12a99-12a100
    Press any key to continue . . .
      

  8.   


                List<string> list = new List<string> { "123", "a123", "aaa", "12a3", "00001", "a00001", "12a99" };
                foreach (string s in list)
                {
                    string result = Regex.Replace(s, @"\d+$", m =>
                    {
                        string str = (Convert.ToInt32(m.Value) + 1).ToString();
                        return str.Length < m.Value.Length ? str.PadLeft(m.Value.Length, '0') : str;
                    });
                    Console.WriteLine(result);
                }
                Console.ReadLine();
    /*
    124
    a124
    aaa
    12a4
    00002
    a00002
    12a100*/