日期:2014-05-18  浏览次数:20397 次

|M| 取一串字符串里里面的数字的问题 谢谢
如有类似如下字符串:
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

谢谢   怎么取最好

------解决方案--------------------
呵呵 笨点方法 找 “元”字符
------解决方案--------------------
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;
}
------解决方案--------------------
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]);