C# 在asp.net c中查找两个指定字符之间的字符串#
在这里,我只需要从这些中分离出246925795 id和&authType之间的字符串在哪里请有人帮助我C# 在asp.net c中查找两个指定字符之间的字符串#,c#,asp.net,C#,Asp.net,在这里,我只需要从这些中分离出246925795 id和&authType之间的字符串在哪里请有人帮助我 string url = "https://www.dgdgfdgdfg.com/profile/view?id=246925795&authType=name&authToken=sygl&trk=prof-sb-browse_map-name"; int a=url.IndexOf('='); int b=url.I
string url = "https://www.dgdgfdgdfg.com/profile/view?id=246925795&authType=name&authToken=sygl&trk=prof-sb-browse_map-name";
int a=url.IndexOf('=');
int b=url.IndexOf('&');
if(a!=-1 && b!=-1)
{
int id = Int32.Parse(url.Substring(a + 1, (b - a) - 1));
}
试试这个:
int start = str.IndexOf("id=") + "id=".Length;
int end = str.IndexOf("&authType");
var result = str.Substring(start, end-start);
更好的方法是将URL更改为URI的对象表示形式,并方便地访问URI的各个部分
string s = @"https://www.dgdgfdgdfg.com/profile/view?id=246925795&authType=name&authToken=sygl&trk=prof-sb-browse_map-name";
Uri myUri = new Uri(s);
string sId = HttpUtility.ParseQueryString(myUri.Query).Get("id"); //246925795
这些是url中的查询字符串。你们可以展示一些代码,你们已经尝试过了吗?事实上,我正在使用文本框插入多个链接,而插入时我需要检查使用id,这些链接在url中,这里只有id是公共的,在我检查完整url之前,我有两个或多个相同的url。你们在字符串变量中有这个url吗?否则如果(!string.IsNullOrEmpty(txtmultilies.Text)){lblcorrect.Text=“”;lblerror.Text=“”;string txt=txtmultilines.Text;string[]lst=txt.Split(新字符[]{'\n','\r'},StringSplitOptions.removemptyEntries);foreach(lst中的字符串元素){//Label2.Text=“”;Label2.Text+=element;感谢brother的友好帮助,只有在
id=
之前不存在以id
结尾的其他参数时,以及当id=
实际存在时,当id={id}
后面紧跟着的是&authType
。不可维护、不健壮、过于具体的答案对OP以外的任何人都没有好处,最糟糕的是:重新发明轮子..NET有一个完美的URL解析器,请参见。-1。@Hassansar,感谢您的更新:)