大家帮帮忙!一个字符串提取问题!

h725895 2009-03-25 06:15:59
我在CMD 里PING 域名获取了一串字符,如下:
Pinging www.01zb.com [210.51.191.198] with 32 bytes of data:

Reply from 210.51.191.198: bytes=32 time=58ms TTL=51
Reply from 210.51.191.198: bytes=32 time=54ms TTL=51
Reply from 210.51.191.198: bytes=32 time=119ms TTL=51
Reply from 210.51.191.198: bytes=32 time=52ms TTL=51

Ping statistics for 210.51.191.198:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 52ms, Maximum = 119ms, Average = 70ms

我只想要time的四个值 其它的都不要!意思就是想要:
time=58ms
time=54ms
time=119ms
time=52ms

但自己想了好久,没想出来怎么裁取好!
大家帮下!谢谢!
...全文
47 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
指间的风 2009-03-25
  • 打赏
  • 举报
回复
这下完整了,复制 调用 OK

private string PingTime(string PingStr)
{
string tempStr = "";
tempStr = PingStr.Substring((text.LastIndexOf("time")));
string[] tempArray = tempStr.Split(' ');
return tempArray[0];
}
指间的风 2009-03-25
  • 打赏
  • 举报
回复
我认为做简单的 做法是

string temp = "";
temp = text.Substring((text.LastIndexOf("time")));
string[] temparray = temp.Split(' ');
return temparray[0];
宝_爸 2009-03-25
  • 打赏
  • 举报
回复
至于如何在C#中执行cmd命令,并取得结果,参见下面的文章

在.net中悄悄执行dos命令,并获取执行的结果
http://www.cnblogs.com/HurYun/archive/2006/09/20/509280.html
teerhu 2009-03-25
  • 打赏
  • 举报
回复
protected string test(string str)
for(i=0;i <strs.length;i++)
{
string[] strs=str.Split(' '); //以空格作分隔符
if(strs[i].StartWith("time"))
{
return strs[i];
}
}
宝_爸 2009-03-25
  • 打赏
  • 举报
回复
使用假设ping的结果保存在string strSource中。
List<string> res = new List<string>();

int beginPos = 0;
int endPos =0;
while(true)
{
beginPos = strSource.IndexOf("time=", beginPos);
if(beginPos == -1)
break;

endPos = strSource.IndexOf(" ", beginPos);
string oneTime = strSource.SubString(beginPos, endPos - beginPos);
res.Add(oneTime);
begionPos = endPos + 1;
}

只是思路,没有编译过。
teerhu 2009-03-25
  • 打赏
  • 举报
回复

protected string test(string str)
for(i=0;i<strs.length;i++)
{
string[] strs=str.split(' ');
if(strs[i].Contains("time")
{
return strs[i];
}
}

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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