string str = "0D4100103"; int i = 0; foreach (char c in str) { i++; if (char.IsLetter(c)) { str = str.Remove(i - 1, 1); } }
using System; using System.Text.RegularExpressions;//要用正则表达式必须引用这个命名空间 using System.Windows.Forms;namespace WindowsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = "0D4100103"; string test = str.Substring(0, Regex.Match(str, "[a-zA-Z]").Index);//前半部份,不包含字母 string test1 = str.Substring(0, Regex.Match(str, "[a-zA-Z]").Index+1);//前半部份,包含字母 string test2 = str.Substring(Regex.Match(str, "[a-zA-Z]").Index);//后半部份,包含字母 string test3 = str.Substring(Regex.Match(str, "[a-zA-Z]").Index + 1);//后半部份,不包含字母 MessageBox.Show(this, test, "前半部份,不包含字母"); MessageBox.Show(this, test1, "前半部份,包含字母"); MessageBox.Show(this, test2, "后半部份,包含字母"); MessageBox.Show(this, test3, "后半部份,不包含字母"); } } }
string s = "0D4100103"; for (int i = 0; i < s.Length; i++) { if (s[i] > 'A') { Console.WriteLine(s[i]); Console.WriteLine(i); break; } } Console.Read();
string s = "0D4100103"; for (int i = 0; i < s.Length; i++) { if (s[i] > 'A') { Console.WriteLine(s[i]); Console.WriteLine(i); break; } } Console.Read();
string s = "0D4100103"; for (int i = 0; i < s.Length; i++) { if (s[i] >= 'A') { Console.WriteLine(s[i]); Console.WriteLine(i); break; } } Console.Read();
第三个例子 D S 是Exception了?如果例子错了……那啥 挨个字符去比对取得'A' 'Z'之间的,就返回下标。
string str = "01D4100103"; int index = 0 ; Regex reg = new Regex("^[a-zA-Z]$"); for (int i = 0; i < str.Length; i++) { if (reg.IsMatch(str[i].ToString())) { index = i; break; } } string[] strs = str.Split(str[index]); for (int i = 0; i < strs.Length; i++) { Console.WriteLine(strs[i]);
void Main()
{
var list=new List<string>
{
"0D4100103",
"004P00103",
"0D41S0103"
};
list.ForEach(l=>Console.WriteLine(Regex.Match(l,"[a-zA-Z]").Index));
/*
1
3
1
*/
}
不好意思:
是变数:0D4100103
004P00303
0041S0603
string str = "0D4100103";
int i = 0;
foreach (char c in str)
{
i++;
if (char.IsLetter(c))
{
str = str.Remove(i - 1, 1);
}
}
using System;
using System.Text.RegularExpressions;//要用正则表达式必须引用这个命名空间
using System.Windows.Forms;namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string str = "0D4100103";
string test = str.Substring(0, Regex.Match(str, "[a-zA-Z]").Index);//前半部份,不包含字母
string test1 = str.Substring(0, Regex.Match(str, "[a-zA-Z]").Index+1);//前半部份,包含字母
string test2 = str.Substring(Regex.Match(str, "[a-zA-Z]").Index);//后半部份,包含字母
string test3 = str.Substring(Regex.Match(str, "[a-zA-Z]").Index + 1);//后半部份,不包含字母
MessageBox.Show(this, test, "前半部份,不包含字母");
MessageBox.Show(this, test1, "前半部份,包含字母");
MessageBox.Show(this, test2, "后半部份,包含字母");
MessageBox.Show(this, test3, "后半部份,不包含字母");
}
}
}
for (int i = 0; i < s.Length; i++)
{
if (s[i] > 'A')
{
Console.WriteLine(s[i]);
Console.WriteLine(i);
break;
}
}
Console.Read();
for (int i = 0; i < s.Length; i++)
{
if (s[i] > 'A')
{
Console.WriteLine(s[i]);
Console.WriteLine(i);
break;
}
}
Console.Read();
for (int i = 0; i < s.Length; i++)
{
if (s[i] >= 'A')
{
Console.WriteLine(s[i]);
Console.WriteLine(i);
break;
}
}
Console.Read();
Console.WriteLine(Regex.Match(input,@"^\d*").Value.Length);
int index = 0 ;
Regex reg = new Regex("^[a-zA-Z]$");
for (int i = 0; i < str.Length; i++)
{ if (reg.IsMatch(str[i].ToString()))
{
index = i;
break;
}
}
string[] strs = str.Split(str[index]);
for (int i = 0; i < strs.Length; i++)
{
Console.WriteLine(strs[i]);
}