如有类似如下字符串:
Str="
(单程)广州-北京 CZ345 L舱 净价(不含燃油机建)1020元  航班起飞(日期/时间)2007-07-25/10:00"也有可以是
Str="
(往返)广州-北京-广州 CZ3101|CA1351 M|H舱 净价(不含燃油机建)1280元|1450元 航班起飞(日期/时间)2007-07-11/08:00|2007-07-12/07:50"然后现在有两个变量
Decimal Price1 = 0
Decimal Price2 = 0
现在要求是 当是往返的时候 也就是会出现1280元|1450元这样的时候
Price1=1280
Price2=1450不是的他就为
Price1=1020
Price2=0谢谢 怎么取最好

解决方案 »

  1.   

    if(Str.Split('元')>2)
    {
    string s = Str.Substring(Str.IndexOf("建)")+2,Str.lastIndexOf("元")-Str.IndexOf("建)")-2);
    string[] ss = s.Split('|');
    price1 = Convert.ToDecimal(ss[0].Replace("元",""));
    price2 = Convert.ToDecimal(ss[1].Replace("元",""));
    }
    else
    {
    price1 = Convert.ToDecimal(Str.Substring(Str.IndexOf("建)")+2,Str.IndexOf("元")-Str.IndexOf("建)")-2));
    price2 = 0;
    }
      

  2.   

    string Str="(往返)广州-北京-广州 CZ3101|CA1351 M|H舱 净价(不含燃油机建)1280元|1450元 航班起飞(日期/时间)2007-07-11/08:00|2007-07-12/07:50";
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"(\d+)元");
            System.Text.RegularExpressions.MatchCollection ms =reg.Matches(Str);
            string[] price  = new string[2];
            if(ms.Count  == 2)
            {
                price[0] = ms[0].Result("$1");
                price[1] = ms[1].Result("$1");
            }
            else  if(ms.Count == 1)
            {
                price[0] = ms[0].Result("$1");
                price[1]= "0";
            }
            Response.Write(price[0]+"<BR>");
            Response.Write(price[1]);
      

  3.   

    //string sourceString1 = @"(往返)广州-北京-广州 CZ3101|CA1351 M|H舱 净价(不含燃油机建)1280元|1450元 航班起飞(日期/时间)2007-07-11/08:00|2007-07-12/07:50";
                string sourceString2 = @"(单程)广州-北京 CZ345 L舱 净价(不含燃油机建)1020元  航班起飞(日期/时间)2007-07-25/10:00";
                //string pattern1 = @"(?<p1>\d*)元(\|)(?<p2>\d*)元([\s\S]*)";
                string pattern2 = @"(?<p1>\d*)元([\s\S]*)";            Match mr = Regex.Match(sourceString2, pattern2, RegexOptions.IgnoreCase);
                string result = string.Empty;
                if (mr.Success)
                {
                    if (mr.Groups["p1"].Value != "")
                    {
                        result += mr.Groups["p1"].Value;
                    }
                    if (mr.Groups["p2"].Value != "")
                    {
                        result += mr.Groups["p2"].Value;
                    }
                }
                this.TextBox1.Text = result;根据需要取sourceString1或sourceString2就是了。
      
    ---------------------------------------------
    MSN:[email protected]请给我一个与您交流的机会!