C#字符串问题。。。。求助

zsm5354 2012-03-23 05:27:10
假定1.txt的内容为
13 渝C15X41 2012.01 13725153XX6
13 渝C15X18 2012.02 13326251XX5
13 渝C15X17 2012.02 13300032XX6
02 渝CK2X55 2011.12 13708345XX8
02 渝AU1X75 2012.01 13896898XX5
02 渝G52X39 2012.02 13896792XX8
string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i < txt.Length; i++)
{
//假如我想知道第二列,也就是渝XXXXXX的长度是不是=6,渝不算,请问怎么写?
}
{
...全文
130 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 zsm5354 的回复:]

引用 12 楼 chinajiyong 的回复:
引用 11 楼 zsm5354 的回复:

引用 10 楼 chinajiyong 的回复:
没有渝字,可以为空吗

C# code

string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i <……
[/Quote]
推荐我总结的
http://topic.csdn.net/u/20120228/11/88a6fd52-de02-4c3e-9f97-a1ffe2fbab24.html
zsm5354 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 chinajiyong 的回复:]
引用 11 楼 zsm5354 的回复:

引用 10 楼 chinajiyong 的回复:
没有渝字,可以为空吗

C# code

string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i < txt.Length; i++)
{
Regex……
[/Quote]
谢谢大神了,有空也学习下正则
EnForGrass 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zsm5354 的回复:]

引用 10 楼 chinajiyong 的回复:
没有渝字,可以为空吗

C# code

string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i < txt.Length; i++)
{
Regex reg = new Regex(@"(?i)……
[/Quote]
判断长度在代码写不就是Length,非要用正则也行,我只考虑长度,至于手机号码正则就没判断
if(Regex.IsMatch(txt[i],@"^\d{11}$"))//满足长度为11
{
}
zsm5354 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chinajiyong 的回复:]
没有渝字,可以为空吗

C# code

string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i < txt.Length; i++)
{
Regex reg = new Regex(@"(?i)(?<=\b[渝]*)\S{6……
[/Quote]
这个可以有,能在请教一下如果判断手机长度是不是11个数字呢?谢谢
EnForGrass 2012-03-23
  • 打赏
  • 举报
回复
没有渝字,可以为空吗

string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i < txt.Length; i++)
{
Regex reg = new Regex(@"(?i)(?<=\b[渝]*)\S{6}\b");
Match mm = reg.Match(txt[i]);
if (mm.Success)
{
MessageBox.Show("Lenght is 6");
}
else
{
}
}
zsm5354 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chinajiyong 的回复:]
C# code

string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i < txt.Length; i++)
{
Regex reg = new Regex(@"渝\S{6}");
Match mm =……
[/Quote]
那请问如果没有那个“渝”字呢?谢谢了
zsm5354 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
对每一行应用正则

C# code

string source = @"渝C15X18 2012.02 13326251XX5";
Regex reg = new Regex(@"渝[\S]{6}");
Match mm = reg.Match(source);
if (mm.Succe……
[/Quote]
那请问如果没有那个“渝”字呢?谢谢了
  • 打赏
  • 举报
回复
不知道你们测试过了没有,如果没有结束标记7位的也会匹配成功,上面的会有问题
string pattern = @"(?i)(?<=[\u4e00-\u9fa5]\s*)\S{6}(?=\s+)";

foreach (string row in File.ReadLines(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312")))
{
if (Regex.IsMatch(row, pattern))
{

}
}
EnForGrass 2012-03-23
  • 打赏
  • 举报
回复

string[] txt = File.ReadAllLines(@"C:\1.txt),Encoding.GetEncoding("GB2312"));
for (int i = 0; i < txt.Length; i++)
{
Regex reg = new Regex(@"渝\S{6}");
Match mm = reg.Match(txt[i]);
if (mm.Success)
{
MessageBox.Show("Lenght is 6");
}
else
{
}
}
  • 打赏
  • 举报
回复
+1[Quote=引用 1 楼 bdmh 的回复:]

对每一行应用正则
C# code

string source = @"渝C15X18 2012.02 13326251XX5";
Regex reg = new Regex(@"渝[\S]{6}");
Match mm = reg.Match(source);
if (mm.Success)
……
[/Quote]
faoyy 2012-03-23
  • 打赏
  • 举报
回复
string[] ss = txt.split(' ');

if(ss[1].length -1 == 6)
{}

这是假设前面只有一个汉字,如果多个汉字,要多些判断
faoyy 2012-03-23
  • 打赏
  • 举报
回复
string[] ss =txt[i].split(' ')

if(ss[1].length-1 == 6)
{}

这是假设前面只有一个汉字,如果你想要从非汉子开始那就再多个判断
还想懒够 2012-03-23
  • 打赏
  • 举报
回复
每一行.Split(' ')[1]
bdmh 2012-03-23
  • 打赏
  • 举报
回复
对每一行应用正则

string source = @"渝C15X18 2012.02 13326251XX5";
Regex reg = new Regex(@"渝[\S]{6}");
Match mm = reg.Match(source);
if (mm.Success)
{
MessageBox.Show("ok");
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧