求一个解答,谢谢。在线等

smartcoach 2012-07-04 10:43:50
有一组数据,我想用循环的方法foreach来找出其中以68开始16结束的这样的字符串,可是新手代码不成功啊,求各位指导
...全文
162 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
直接用系统自带的方法就是了、既然不会用正则
[Quote=引用 2 楼 的回复:]
C# code


string[] arr = new string[] { "683316", "b" };
foreach (string s in arr)
{
if (s.StartsWith("68") && s.EndsWith("16"))
……
[/Quote]
smartcoach 2012-07-05
  • 打赏
  • 举报
回复
还得麻烦您,您这样的写法是可以但是我要处理的数据是很大的一堆,不能都{ "683316", "b" };
的写啊,怎么把那一堆数放到数组里呢?[Quote=引用 2 楼 的回复:]
C# code

string[] arr = new string[] { "683316", "b" };
foreach (string s in arr)
{
if (s.StartsWith("68") && s.EndsWith("16"))
……
[/Quote]
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

请问{"6812" ,"6816","2316","","68SP16"};
是什么意思啊?而且错误 1 当前上下文中不存在名称“Regex” 引用 1 楼 的回复:
C# code

List<string> list = new List<string>() {"6812" ,"6816","2316","","68SP16"};
List<string> result = ne……
[/Quote]
Regex是正则处理用的类,添加个using就没有问题了
jakwos 2012-07-04
  • 打赏
  • 举报
回复
3楼没错,要引用命名空间,using System.Text.RegularExpressions;
youzelin 2012-07-04
  • 打赏
  • 举报
回复
如果楼主不太会用正则表达式,可以用以下两个函数来判定,效果一样:

"......".StartWith("68");
"......".EndWith("16");
smartcoach 2012-07-04
  • 打赏
  • 举报
回复
请问{"6812" ,"6816","2316","","68SP16"};
是什么意思啊?而且错误 1 当前上下文中不存在名称“Regex” [Quote=引用 1 楼 的回复:]
C# code

List<string> list = new List<string>() {"6812" ,"6816","2316","","68SP16"};
List<string> result = new List<string>();
string pattern = @"^68.*?16$";
……
[/Quote]
bdmh 2012-07-04
  • 打赏
  • 举报
回复

string[] arr = new string[] { "683316", "b" };
foreach (string s in arr)
{
if (s.StartsWith("68") && s.EndsWith("16"))
{
MessageBox.Show(s);
}
}
  • 打赏
  • 举报
回复
    List<string> list = new List<string>() {"6812" ,"6816","2316","","68SP16"};
List<string> result = new List<string>();
string pattern = @"^68.*?16$";
foreach (string item in list)
{
if (Regex.IsMatch(item, pattern))
{
result.Add(item);
}
}
/*
* [0] "6816" string
[1] "68SP16" string

*/

110,536

社区成员

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

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

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