private string[] CutStr(string InPut)
    {
       //todo
    }
要求:把字符串以数字为界分割存入数组例如:
 String Str="减肥的324的方式将67地方45发达";
 String[] mm= CutStr(Str);
mm[0]="减肥的";
mm[1]="324";
mm[2]="的方式将";
mm[3]="67";
mm[4]="地方";
mm[5="45";
mm[6]="发达";

解决方案 »

  1.   


    String Str="减肥的324的方式将67地方45发达";
    MatchCollection mc = Regex.Matches(Str,@"\d+|\D+");
    string[] mm = new string[mc.Count];
    for(int i=0;i<mc.Count;i++)
    {
        mm[i] = mc[i].Value;
    }
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    namespace Program
    {
        class Program
        {
            static void Main(string[] args)
            {
                String Str = "减肥的324的方式将67地方45发达";
                MatchCollection mc = Regex.Matches(Str, @"\d+|\D+");
                string[] mm = new string[mc.Count];
                for (int i = 0; i < mc.Count; i++)
                {
                    mm[i] = mc[i].Value;
                    Console.WriteLine(mm[i]);
                }
            }
        }
    }楼上两位仁兄的方法是正解
      

  3.   

    Regex regex = new Regex(@"\d+");
    MatchCollection ms = regex.Matches("");
    foreach(Match m in ms)
    {
      Console.WriteLine(m.Value.ToString());
    }
    或Reges.Replace("",@"\d+",",")替换分割
      

  4.   

    try...private string[] CutStr(string InPut)
    {
        Regex reg = new Regex( @"(?<!^)\b(?!$)", RegexOptions.ECMAScript);
        return reg.Split(InPut);
    }
      

  5.   


    汗,无牙,不要乱叫哈,害得我都不敢回帖了其实这种写法在我的博客里很早就已经有了,只不过使用\b,如果楼主的源字符串中再掺杂些字母之类的,得到的就不一定是楼主想要的结果了之前倒是想玩些花样
    private string[] CutStr(string InPut)
    {
        Regex reg = new Regex(@"(?<=(\d)|\D)(?=(?(1)\D|\d))");
        return reg.Split(InPut);
    }
    不过这样就会把捕捉组的内容也存到结果数组中去,Split方法中又没有提供这种参数可以不捕获的//这样写又太土了
    private string[] CutStr(string InPut)
    {
        Regex reg = new Regex(@"(?<=\d)(?=\D)|(?<=\D)(?=\d)");
        return reg.Split(InPut);
    }
      

  6.   

    String Str = "减肥的324的方式将67地方45发达";
                MatchCollection mc = Regex.Matches(Str, @"\d+|\D+");
                string[] mm = new string[mc.Count];
                for (int i = 0; i < mc.Count; i++)
                {
                    mm[i] = mc[i].Value;
                    Console.WriteLine(mm[i]);
                }