Regex r = new Regex(@"\d"); int i = 0; for (int k = 0; k < txt12.Length; k++) { if (r.IsMatch(txt12)) { break; } i++; }。
/// <summary> /// 返回指定字符串第一次出现数字的位置,如果未找到返回-1. /// </summary> public int IndexOfFirstNumber(string text) { if (string.IsNullOrEmpty(text)) return -1; int i, length = text.length; for (i = 0; i < length; i++) { char ch = text[i]; if (char.IsDigit(ch)) break; } return i == length ? -1 : i; }
用不着这么麻烦 Regex r = new Regex(@"\d"); int index = r.Match( txt12 ).Index;
如果找不到,则得到-1: string s = "txt12"; int index=-1; for(int i=0;i<s.Length;i++) if (char.IsDigit(s[i])) { index = i; break; } Response.Write(index);
int index = "txt12".IndexOf("1");
好吧,没注意不成功的情况 Regex r = new Regex(@"\d"); Match m = r.Match( txt12 ); return m.Success ? m.Index : -1;
int i = 0;
for (int k = 0; k < txt12.Length; k++)
{
if (r.IsMatch(txt12))
{
break;
}
i++;
}。
/// <summary>
/// 返回指定字符串第一次出现数字的位置,如果未找到返回-1.
/// </summary>
public int IndexOfFirstNumber(string text)
{
if (string.IsNullOrEmpty(text))
return -1; int i, length = text.length; for (i = 0; i < length; i++)
{
char ch = text[i];
if (char.IsDigit(ch)) break;
} return i == length ? -1 : i;
}
Regex r = new Regex(@"\d");
int index = r.Match( txt12 ).Index;
string s = "txt12";
int index=-1;
for(int i=0;i<s.Length;i++)
if (char.IsDigit(s[i]))
{
index = i;
break;
}
Response.Write(index);
Regex r = new Regex(@"\d");
Match m = r.Match( txt12 );
return m.Success ? m.Index : -1;
不过要学会查文档倒是真的,这些都是在MSDN中可以找到的