求助,Substring()的问题 例如:Qu中的值是(你好 10,他好 11)int Quantity;int posH;posH = Qu.IndexOf(",");问题:如何让Quantity = Qu中的数字 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string str = "你好 10,他好 11"; Regex reg = new Regex(@"(?<=[^,,]+)\d+"); Response.Write(reg.Match(str));//10 Quantity是String类型int posH;posH = Qu.IndexOf(",");//Qu中的值是(你好 10,他好 11)Quantity=? //这里该怎么写?才能让Quantity的值是(10,11)Qu = Qu.Substring(posH + 1);//这步是用来区分10跟11的。 string str="(你好 10,他好 11)"; string qu = Regex.Replace(str, @"[^\d,,\(\)()]+", ""); Response.Write(qu);//(10,11) code C# code public static class Common { //isDigit public static bool isNumberic1(this string _string) { if (string.IsNullOrEmpty(_string)) return false; foreach (char c in _string) { if (!char.IsDigit(c)) return false; } return true; } //vb isnumberic public static bool isNumberic2(this string _string) { return !string.IsNullOrEmpty(_string) && Microsoft.VisualBasic.Information.IsNumeric(_string); } //try parese public static bool isNumberic3(this string _string) { if (string.IsNullOrEmpty(_string)) return false; int i = 0; return int.TryParse(_string, out i); } //try catch public static bool isNumberic4(this string _string) { if (string.IsNullOrEmpty(_string)) return false; int i = 0; try { int.Parse(_string); } catch { return false; } return true; } //regex public static bool isNumberic5(this string _string) { return !string.IsNullOrEmpty(_string) && Regex.IsMatch(_string, "^\\d+$"); } } 测试的代码:code C# code class Program { static void Main(string[] args) { Test("1234"); Test("1234a"); Test("a1234"); Test(""); Test(null); } static void Test(string str) { bool res1 = false, res2 = false, res3 = false, res4 = false, res5 = false; Stopwatch wat = new Stopwatch(); wat.Start(); for (int i = 1; i < 100000; i++) { res1 = str.isNumberic1(); } wat.Stop(); Console.WriteLine("isDigit {0}:{1},{2}", str, wat.ElapsedMilliseconds, res1); wat.Reset(); wat.Start(); for (int i = 1; i < 100000; i++) { res2= str.isNumberic2(); } wat.Stop(); Console.WriteLine("isNumberic {0}:{1},{2}", str, wat.ElapsedMilliseconds, res2); wat.Reset(); wat.Start(); for (int i = 1; i < 100000; i++) { res3 = str.isNumberic3(); } wat.Stop(); Console.WriteLine("try parse {0}:{1},{2}", str, wat.ElapsedMilliseconds, res3); wat.Reset(); wat.Start(); for (int i = 1; i < 100000; i++) { res4 = str.isNumberic4(); } wat.Stop(); Console.WriteLine("try catch {0}:{1},{2}", str, wat.ElapsedMilliseconds, res4); wat.Reset(); wat.Start(); for (int i = 1; i < 100000; i++) { res5 = str.isNumberic5(); } wat.Stop(); Console.WriteLine("regex {0}:{1},{2}", str, wat.ElapsedMilliseconds, res5); Console.WriteLine(); } } 这里还有个问题,就是如果string str=="(你好1 10,他好2 11)";最后Response.Write(qu);qu的值不就是(110,211)吗! 自画cxgrid表格,当关闭第二窗口时出现部分空白如何解决? excel导出时卡住不动了 [问题]令超链接<a>标签触发事件 从一个数据库提取两张表的内容 这样的显示 求一正则 200分,求教VS2003打包若干问题! 请问有mdf 和ldf 文件 怎么样让他还原到mssql 里面啊??在线等 CTI来电显示的问题! 两个关于自定义控件的问题 字符串去图片的正则 MVC2图片路径的问题
Regex reg = new Regex(@"(?<=[^,,]+)\d+");
Response.Write(reg.Match(str));
//10
int posH;
posH = Qu.IndexOf(",");//Qu中的值是(你好 10,他好 11)
Quantity=? //这里该怎么写?才能让Quantity的值是(10,11)
Qu = Qu.Substring(posH + 1);//这步是用来区分10跟11的。
string qu = Regex.Replace(str, @"[^\d,,\(\)()]+", "");
Response.Write(qu);
//(10,11)
C# code public static class Common
{
//isDigit
public static bool isNumberic1(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
foreach (char c in _string)
{
if (!char.IsDigit(c))
return false;
}
return true;
} //vb isnumberic
public static bool isNumberic2(this string _string)
{
return !string.IsNullOrEmpty(_string) && Microsoft.VisualBasic.Information.IsNumeric(_string);
}
//try parese
public static bool isNumberic3(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
int i = 0;
return int.TryParse(_string, out i);
} //try catch
public static bool isNumberic4(this string _string)
{
if (string.IsNullOrEmpty(_string))
return false;
int i = 0;
try { int.Parse(_string); }
catch { return false; }
return true;
}
//regex
public static bool isNumberic5(this string _string)
{
return !string.IsNullOrEmpty(_string) && Regex.IsMatch(_string, "^\\d+$");
}
}
测试的代码:code
C# code class Program
{
static void Main(string[] args)
{ Test("1234");
Test("1234a");
Test("a1234");
Test("");
Test(null);
} static void Test(string str)
{
bool res1 = false, res2 = false, res3 = false, res4 = false, res5 = false;
Stopwatch wat = new Stopwatch();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res1 = str.isNumberic1();
}
wat.Stop();
Console.WriteLine("isDigit {0}:{1},{2}", str, wat.ElapsedMilliseconds, res1); wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res2= str.isNumberic2();
}
wat.Stop();
Console.WriteLine("isNumberic {0}:{1},{2}", str, wat.ElapsedMilliseconds, res2); wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res3 = str.isNumberic3();
}
wat.Stop();
Console.WriteLine("try parse {0}:{1},{2}", str, wat.ElapsedMilliseconds, res3); wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res4 = str.isNumberic4();
}
wat.Stop();
Console.WriteLine("try catch {0}:{1},{2}", str, wat.ElapsedMilliseconds, res4); wat.Reset();
wat.Start();
for (int i = 1; i < 100000; i++)
{
res5 = str.isNumberic5();
}
wat.Stop();
Console.WriteLine("regex {0}:{1},{2}", str, wat.ElapsedMilliseconds, res5);
Console.WriteLine();
}
}
这里还有个问题,就是如果
string str=="(你好1 10,他好2 11)";
最后Response.Write(qu);
qu的值不就是(110,211)吗!