string里分为数字字符和其他字符两大类 我想用 System.Collections.Queue来存储 需要将连续数字字符做为一个整体(即一个整数,但用字符串表示)装进Queue 非数字字符 也做为个整体(即为一个运算符,但用字符表示)装入Queue 并且不改变原string中字符次序 例如:string str = "234+456";
要求Queue中为"234"、"+"、"456" 谁给我个算法呀 谢谢了 :)

解决方案 »

  1.   

    我覺的在這里處理有些不大好﹐如果可以的話﹐最好是在Post的時候就將數字與其它字符用空格分開﹐
    然后在這里的時候就好辦多了﹕string.split()搞定.
    當然如果沒有辦法在客戶端事先處理﹐再考慮下面的方法:
    一﹑字符串較長
    string stmp
    for(int i=0;i<10;i++)
    {
        stmp.replace(i.tostring()," ");
    }
    現在數字全部變為" "了﹐接著就是將非數字的字符串分開.
    string[] stmp2=stmp.split(" ");
    建立新的字串組﹐保存最后的結果()
    arraylist stmp3;
    for(int i=0;i<stmp2.lengh;i++)
    {
    if(stmp2[i]!="")
    {
    if(stmp.startwith(stmp2[i])==false)
    {
       stmp3.add(stmp.substring(0,stmp.indexof(stmp2[i]))
       stmp=stmp.substring(stmp.indexof(stmp2[i]));
    }
    stmp3.add(stmp2[i])
    stmp=stmp.substring(stmp2[i].length);
    }
    }二﹑如果字符串比較短,就沒有什么好說的了﹐直接判斷是否為數字就行了。將連續的數字保存在一個字串中就行了。
      

  2.   

    我用这个方法老是提示数组越界 实在想不明白哪里有问题
    public string ExpDiv(ref string exp)
    {
    //分解EXP存入public static System.Collections.Queue save 调用一次出列一次 
    string optrStr = "",oprdStr = "";
    for(int i = 0;i < exp.Length;i ++)
    {
    if(!this.IsOptr(exp[i].ToString()))//数字
    {
    exp = exp.Substring(i,exp.Length - i);
    oprdStr += exp[i];
    }
    else if(this.IsOptr(exp[i].ToString()))//算符
    {
    exp = exp.Substring(i,exp.Length - i);//清除存储的数字
    save.Enqueue(oprdStr);
    oprdStr = "";
    try
    {
    optrStr = exp[i].ToString();
    }
    catch(Exception err)
    {
    Console.WriteLine(err.Message);
    }
    save.Enqueue(optrStr);//算符只需一个字符
    }
    //i = 0;
    }
    return save.Dequeue().ToString();
    }
    /*楼上大哥的方法我试试先 呵呵 不知道满足我的要求没有 其实我只是简单地想把算术表达式分离成数字和运算符而已 没想到想了好久都不行呀*/
      

  3.   

    试试我下面的算法,用正则完全可以满足你的要求。
    try {
    Regex RegexObj = new Regex("\\d+|\\D+");
    Match MatchResults = RegexObj.Match(SubjectString);
    while (MatchResults.Success) {

    MatchResults = MatchResults.NextMatch();

    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }
      

  4.   

    楼上大哥能帮我写下吗 
    你的算法我实在不懂用(从来没用过Regex类呀还有Match)
      

  5.   

    Queue q = new Queue();
    foreach(Match m in Regex.Matches("123+456", @"\d+|\D+"))
    {
        q.Enqueue(m.Value);
    }
      

  6.   

    呵呵 果然厉害!
    Regex.Matches()的第二个参数没怎么看的懂 
    而且如果我要的算术表达式是"12*(3-5)-(55-3)"这样类似的带有括号的那么这招还管用吗?
    而事实上要求也是这样的 :)