请教字符串处理的问题(分拆)

pcgreen 2004-08-30 03:52:25
如何快速从这种字符串中取得想要的字符串
原字符串: ||aa:123||bb:222||cc:33
现想定一函数当输入为aa时,则取得123,当输入为cc时则取得:33
当输入不存在的,如dd,则返回空。
在C#中如何快速找到想关值
...全文
163 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
realljx 2004-08-30
  • 打赏
  • 举报
回复
正则表达式的写法。

string a = "||aa:123||bb:222||cc:33";
string xx = "aa";
a+= "||";
string pattern = @"[\s\S]*?\|\|"+xx+@"\:([\s\S]*?)\|\|[\S\s]*";

string res = a;
Match m = Regex.Match(a,pattern);
if(m.Success)
{
a = m.Groups[1].Value;
}
MessageBox.Show(a);
mazekui 2004-08-30
  • 打赏
  • 举报
回复
用法:
string aa=getValue("aa")
mazekui 2004-08-30
  • 打赏
  • 举报
回复
我专门给你写了一个函数如下:
public string getValue(string ze)
{
string old="||aa:123||bb:222||cc:33||dd:4567";
string result="";
string b="",c="";
int len=old.IndexOf(ze);
for(int a=2;a<old.Length;a++)
{
b=old.Substring(a,1);

if(b.Equals(":"))
{
if(c.Equals(ze))
{
c="";
for(int bb=a+1;bb<old.Length;bb++)
{
b=old.Substring(bb,1);
if(b.Equals("|"))
{return c;}
else{c=c+b;}

}
return c;
}
}

else if(b.Equals("|"))
{c="";}
else
{c=c+b;}
}
return result;
}
给分啊!
xiaomaoy 2004-08-30
  • 打赏
  • 举报
回复
先用IndexOf()来查找输入的字符在原字符串中的位置,再用Substring()来从原字符串中截出所需的字符个数即可。
ayya 2004-08-30
  • 打赏
  • 举报
回复
System.Collections里有
wangdequan1024 2004-08-30
  • 打赏
  • 举报
回复
gz
realljx 2004-08-30
  • 打赏
  • 举报
回复
用正则表达式吧。

111,120

社区成员

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

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

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