1.replace对字符串进行替换;
2.判断字符串中是否含有大小写字母string stringA = "123   QQ  qqq";
            if(Regex.IsMatch(stringA, "[A-Z]"))
            {
                Console.WriteLine(stringA.ToLower());
            }

解决方案 »

  1.   

    不知道你想怎么处理输入的Name如果前后有空格的情况,下面的代码是直接去掉用户输入Name的空格。
    如果你的需求不一样,可以自己修改一下。using System;
    using System.Collections.Generic;namespace GetNumber
    {
    class Program
    {
    public static void Main(string[] args)
    {
    // string s = "sample 12";
    // string s = "sample12 ";
    // string s = "sample 12 ";
    string s = "sample1 2";
    // string s = "sample 12a";
    // string s = "sample12b";
    // string s = "sample 12  b8e";
    //Console.WriteLine(string.Format("{0}: {1}", s, GetInt(s)));
    foreach (string name in GetNameList(s, 5)) {
    Console.WriteLine(name);
    }

    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }

    public static List<string> GetNameList(string Name,int count)
    {
    List<string> ret = new List<string>();
    int num = GetInt(Name);
    Name = Name.Trim();
    string prefix = Name.Substring(0, Name.Length - num.ToString().Length);

    for (int i = 0; i < count; ++i) {
    ret.Add(prefix + (num + i + 1).ToString());
    }

    return ret;
    }

    public static int GetInt(string s)
    {
    int ret = 0;
    s = s.Trim();
    int len = s.Length;
    int i;

    for (i = len - 1; len >= 0; --i)
    {
    char c = (char) s[i];
    Console.WriteLine(string.Format("{0}=>{1}", i, s[i])); if (!Char.IsDigit(c)) {
    break;
    }
    }

    Int32.TryParse(s.Substring(i + 1), out ret);
    return ret;
    }
    }
    }
      

  2.   

    不客气,很高兴代码能有帮助
    思路是
    1. 从右边开始判断字符串中的每个字符是否是数字(Char.IsDigit())
    2. 如果非数字,跳出循环,并根据跳出的位置来截取最右边的字符串
    3. 使用Int32.TryParse将截取的字符串转换为整数,方便后面对数字进行+1计算
    4. 从用户输入的Name中去掉截取到的数字部分,作为新名字的前缀
    5. 使用获取的新名字的前缀,通过对前面截取到的数字+1计算,组成新名字
    真的感谢,我自己用了好多判断和循环语句,但总是达不到要的功能,这个代码我再去用心学习下。
      

  3.   

    仅供参考:
    string aa = "sample 12";
                string bb = "sample1 2";
                string cc = "sample 1 2";            var arr1 = bb.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries).ToList();
                var item1 = Convert.ToInt32(arr1.Last());            item1 += 1;            arr1.Remove(arr1.Last());
                arr1.Add(item1.ToString());            var dd = string.Join(" ",arr1);
      

  4.   

    改为
     public static int GetInt(string s)
            {
                int ret = 0;
                s = s.Trim();
                int len = s.Length;
                int i;
                if (!Char.IsDigit(s[len - 1]))
                {
                    Int32.TryParse(s.Substring(len - 1), out ret);
                }
                else
                {
                    for (i = len - 1; len > 0; i--)
                    {
                        if (!Char.IsDigit(s[i]))
                        {
                            ret = Int32.Parse(s.Substring(i+1));
                        }
                    }
                    
                }
                return ret;
            }
      

  5.   

    你的代码这样改有两个问题
    1. if (!Char.IsDigit(s[len - 1]))这句判断最后一个字符为非数字字符,然后再去Int32.TryParse。这段没有必要,因为原先的代码里面也是从最后一个字符判断(也就是你的else块里面),如果是非数字字符会break,跳出循环,最后也执行Int32.TryParse。你加了这个之后,等于最后一个字符串可能要判断两次(当最后一个字符是数字字符的时候)
    2.你在else块里面的for循环里面,判断当字符为非数字字符的时候,你去Int32.Parse,也就是相当于非数字字符去转换成数字,这样肯定会出错的。另外循环没有break,就是要把整个字符串都遍历一遍。而你最初的需求是取出最后面的数字,遇到非数字字符的时候就可以结束判断了。
      

  6.   

    应楼主私信请求,由于无法发送超过200字私信,所以,来这里回复:            /*举例:Name = sample12   ,    count = 3 
                 应该返回List<string> = {sample13,sample14,sample15}
                 判断字符串最后部分的数字时的原则:
                 1).忽略数字部分外的空格:即"sample 12","sample12 ","sample 12 "都认为数字是12;而"sample1 2"则认为是2;
                 2).最后一位不是空格也不是数字,则认为数字是0;如"sample 12a","sample12b","sample 12  b8e"都认为数字是0;*/
                List<string> 生成 = new List<string>();
                Func<string, int> 取数字 = delegate(string 信息)
                {
                    string 字 = "";
                    int 量 = 信息.Length;
                    if (!Char.IsDigit(信息[--量]))
                        字 = "0";
                    else
                        do { if (Char.IsDigit(信息[量])) 字 = 字.Insert(0, 信息[量].ToString()); else break; } while (--量 >= 0);
                    return int.Parse(字);
                };
                Func<string[], int> 转数字 = delegate(string[] 信息)
                {
                    int 判断 = 0, 检 = 0;/*如果转换成功则输出转换值否则等于设定值0*/
                    foreach (string 信 in 信息)
                        if (信 != "") { int.TryParse(信, out 检); ++判断; }/*包含对多空格处理*/
                    if (判断 < 2) 检 = 取数字(信息[0]);
                    return 检;
                };
                Func<string, int, List<string>> 符串最后部分的数字 = delegate(string 信息, int 数量)
                {
                    int 数 = 0, 序 = 0;
                    string[] 拆分 = 信息.Split(' ');
                    序 = 数 = (拆分.Length > 1) ? 转数字(拆分) : 取数字(拆分[0]);
                    do
                    {
                        生成.Add(数 > 0 ? 信息.Replace(数.ToString(), (++序).ToString()) : 信息 + (++序).ToString());
                    } while (--数量 > 0);
                    return 生成;
                };
                符串最后部分的数字("sample11  2   ", 4);
                foreach (string 信息 in 生成)
                    Console.WriteLine(信息);
      

  7.   

    更正一句:
    do { if (Char.IsDigit(信息[量])) 字 = 字.Insert(0, 信息[量].ToString()); else break; } while (--量 > 0);
    写完后想到的,之上代码中忘记改了,虽说目前测试不会发生错误,但知道了不改回来心理总有疙瘩很难受的,改一下心情很好.
      

  8.   

    我是想优化GetInt()这个函数,是、使代码达到13楼显示的那种效果。但写出来的代码,确实出现了你指出的两个问题
      

  9.   

    你是说想把最后的数字和前面的字符前缀自动加上空格吗?否则的话,开始的代码就能实现13楼的效果。
    不知道13楼要对信息.Split(' ');做什么,你不是只需要最后一个非空格的数字吗,对字符串中间的空格还是以空格处理的不是吗?就像你举的例子,"sample1 2"则认为是2。
    你可以举个极端的例子开始的代码不能实现的,可以再看看怎么调整开始的代码纠正。
      

  10.   

    您的代码,在末尾是数字的时候,自动会把数字审定为0.比如simple 12 当count=3时。我希望的输出结果是simple 13,simple 14,simple 15。而您的输出结果是simple 11 simple12 simple 13。所以我对您的代码进行了改动
      

  11.   

    顺便问一下,关于C#的GDI+绘图,会吗
      

  12.   

    我测试的时候,simple 12 当count=3时。输出结果是simple 13,simple 14,simple 15。
    下面的代码你再测一下using System;
    using System.Collections.Generic;namespace GetNumber
    {
    class Program
    {
    public static void Main(string[] args)
    {
    string s = "sample 12";
    // string s = "sample12 ";
    // string s = "sample 12 ";
    // string s = "sample1 2";
    // string s = "sample 12a";
    // string s = "sample12b";
    // string s = "sample 12  b8e";
    //Console.WriteLine(string.Format("{0}: {1}", s, GetInt(s)));

    Console.WriteLine(string.Format("Input: {0}", s));
    PrintName(GetNameList(s, 3));

    Console.WriteLine(string.Format("Input: {0}", s));
    s = "sample 12  b8e";
    PrintName(GetNameList(s, 3));

    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }

    public static void PrintName(List<string> list)
    {
    foreach (string name in list)
    {
    Console.WriteLine(name);
    }
    }

    public static List<string> GetNameList(string Name,int count)
    {
    List<string> ret = new List<string>();
    int num = GetInt(Name);
    Name = Name.Trim();
    string prefix = Name.Substring(0, Name.Length - num.ToString().Length);

    for (int i = 0; i < count; ++i)
    {
    ret.Add(prefix + (num + i + 1).ToString());
    }

    return ret;
    }

    public static int GetInt(string s)
    {
    int ret = 0;
    s = s.Trim();
    int len = s.Length;
    int i;

    for (i = len - 1; len >= 0; --i)
    {
    char c = (char) s[i];
    //Console.WriteLine(string.Format("{0}=>{1}", i, s[i])); if (!Char.IsDigit(c))
    {
    break;
    }
    }

    Int32.TryParse(s.Substring(i + 1), out ret);
    return ret;
    }
    }
    }
      

  13.   

    其实我都不会,我都是现学的。你需要GDI+绘什么图?
      

  14.   

    我在自己制作一个自定义的控件,需要运用到GDI+的控件,大致思路有了,但是没有做过控件,也没有接触过GDI+所以想问问你。