C# 如何判断一个字符串以400,800,1开头

gdk123 2012-06-12 10:40:13
如题:
...全文
479 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdk123 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
如果你是想判断一个字符串以800或者400或者1开头的话,可以写一个扩展方法:

C# code


public static class Ext
{
public static bool StartsWith(this string s, params string[] prefix)
{
return ……
[/Quote]

prefix.Any()是什么意思?
水牛刀刀 2012-06-12
  • 打赏
  • 举报
回复
如果你是想判断一个字符串以800或者400或者1开头的话,可以写一个扩展方法:

public static class Ext
{
public static bool StartsWith(this string s, params string[] prefix)
{
return prefix.Any(p => s.StartsWith(p));
}
}
//用法:
bool result = s.StartsWith("1", "400", "800", "30"); //是否以1或者400或者800或者30开头

bdmh 2012-06-12
  • 打赏
  • 举报
回复
string的 StartsWith 判断,或者
正则

Regex reg = new Regex(@"^(800|400|1).*");
EnForGrass 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

取这个字符串的前9位和400,800,1比较
也可以用正则
[/Quote]
貌似string.StartsWith()方法

string s = "400,800,1fdasdfasfas2423vcsa";
bool isTrue=s.StartsWith("400,800,1");
EnForGrass 2012-06-12
  • 打赏
  • 举报
回复
取这个字符串的前9位和400,800,1比较
也可以用正则
mizuho_2006 2012-06-12
  • 打赏
  • 举报
回复
if(str.StartWith("400,800,1"))
cnfixit 2012-06-12
  • 打赏
  • 举报
回复
StartsWith
__天涯寻梦 2012-06-12
  • 打赏
  • 举报
回复
string str = "400526398|800046846|12365478965|33444425";
string[] arr = str.Split('|');
if (arr[0].StartsWith("400") && arr[1].StartsWith("800") && arr[2].StartsWith("1"))
{
arr[3] = "110" + arr[3];
string result = string.Join("|", arr);
}
gdk123 2012-06-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
Loop:
接收
拆解
操作
组合
发送
[/Quote]

这个,文字可以翻译成代码不?
铜臂阿铁木 2012-06-12
  • 打赏
  • 举报
回复
Loop:
接收
拆解
操作
组合
发送
gdk123 2012-06-12
  • 打赏
  • 举报
回复
C# 如何判断一个字符串以400,800,1开头。如果这个字符串的格式为400526398|800046846|12365478965|33444425,如何给“33444425”加上固定的前缀,如:11033444425 然后在返回处理完成的字符串。返回的字符串格式为400526398|800046846|12365478965|11033444425


请教各位高手。。。。
nikolaichow 2012-06-12
  • 打赏
  • 举报
回复
StartsWith简单

110,561

社区成员

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

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

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