C# 如何找到字符串的另一部分
我正在尝试制作一个C#程序,在网站上获得一条线并使用它。 不幸的是,我不知道网站上的全部内容。我只知道“C# 如何找到字符串的另一部分,c#,C#,我正在尝试制作一个C#程序,在网站上获得一条线并使用它。 不幸的是,我不知道网站上的全部内容。我只知道“steam://joinlobby/730/”。尽管如此,“/730/”之后的内容总是不同的 所以我需要你帮我弄清楚后面的全部内容 我得到的是: public void Main() { WebClient web = new WebClient(); // here is the site that i want to download and read text from
steam://joinlobby/730/
”。尽管如此,“/730/
”之后的内容总是不同的
所以我需要你帮我弄清楚后面的全部内容
我得到的是:
public void Main()
{
WebClient web = new WebClient();
// here is the site that i want to download and read text from it.
string result = web.DownloadString("http://steamcommunity.com/id/peppahtank");
if (result.Contains("steam://joinlobby/730/"))
{
//get the part after /730/
}
}
我可以告诉你,它总是以“xxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxx
”结尾
所以:
steam://joinlobby/730/xxxxxxxxx/xxxxxxxx
对于这种特殊情况,最简单的方法是取第一部分,然后跳过那么多字符
const string Prefix = @"steam://joinlobby/730/";
//...
if(result.StartsWith(Prefix))
{
var otherPart = result.SubString(Prefix.Length);
// TODO: Process other part
}
对于这种特殊情况,最简单的方法是取第一部分,然后跳过那么多字符
const string Prefix = @"steam://joinlobby/730/";
//...
if(result.StartsWith(Prefix))
{
var otherPart = result.SubString(Prefix.Length);
// TODO: Process other part
}
有什么方法可以防止您只拆分“/730/”上的字符串
result.Split(@"/730/")[1]
有什么方法可以防止您只拆分“/730/”上的字符串
result.Split(@"/730/")[1]
确保结果不为空,并以
steam://joinlobby/730/
if(string.IsNullOrWhiteSpaces(result) && result.StartsWith("steam://joinlobby/730/"))
{
string rest = result.SubString(("steam://joinlobby/730/").Length);
}
确保结果不为空,并以
steam://joinlobby/730/
if(string.IsNullOrWhiteSpaces(result) && result.StartsWith("steam://joinlobby/730/"))
{
string rest = result.SubString(("steam://joinlobby/730/").Length);
}
你是说像这样的?string strippedResult=result.Replace(“steam://joinlobby/730/", ""); 这将返回steam://joinlobby/730/xxxxxxxxxxxxxxxxx/xxxxxxxxxxxxx 没有steam://joinlobby/730/ 可能有帮助这是一个
URI
。有一个Uri
类可以帮你做到这一点“有些人在遇到问题时,会想“我知道,我会使用正则表达式。”现在他们有两个问题。“你是说这样的吗?string strippedResult=result.Replace(“steam://joinlobby/730/", ""); 这将返回steam://joinlobby/730/xxxxxxxxxxxxxxxxx/xxxxxxxxxxxxx 没有steam://joinlobby/730/ 可能有帮助这是一个URI
。有一个Uri
类可以为您做到这一点“有些人在遇到问题时,会想“我知道,我会使用正则表达式。”现在他们有两个问题。谢谢大家的回答。尽管如此,我现在正在尝试:WebClient web=newwebclient();字符串结果=web.DownloadString(“);常量字符串前缀=@”steam://joinlobby/730/“if(result.StartsWith(Prefix)){var otherPart=result.Substring(Prefix.Length);MessageBox.Show(otherPart);//查看是否实际得到正确的响应}”似乎我只是没有打开一个消息框。我可能在做一些愚蠢的事情…可能还有其他问题-最好将它们隔离并打开另一个问题。而且,没有格式,这些代码是GobbleyBook-如果您必须在注释中包含代码,请使用代码粘贴工具谢谢大家的响应。不过,我现在正在尝试:WebClient web=new WebClient();string result=web.DownloadString(“);常量字符串前缀=@“steam://joinlobby/730/"; 如果(result.StartsWith(Prefix)){var otherPart=result.Substring(Prefix.Length);MessageBox.Show(otherPart);//看我是否真的得到了正确的响应},我似乎只是没有打开MessageBox。我可能在做一些愚蠢的事情……可能还有其他问题——最好把它们孤立起来,然后再问一个问题。另外,如果没有格式化,代码就是GobbleyBook——如果必须在注释中包含代码,请使用代码粘贴工具