另一种解决办法提供了以下优势:
- Does not require the use of regular expressions.
- Does not require a certain count of slashes be present (indexing based of a specific number). I consider this a key benefit because it makes the code less likely to fail if some part of the URL changes. Ultimately it is best to base your parsing logic off which part of the text s structure you consider least likely to change.
This method, however, DOES rely on the following assumptions, which I consider to be the least likely to change:
- URL must have "/Lists/" right before target text.
- URL must have "/" right after target text.
基本上,我只用我预期会环绕我感兴趣的地区的文本,两度分开。
String urlToSearch = "http://example.com/TIGS/SIM/Lists/Team Discussion/DispForm.aspx";
String result = "";
// First, get everthing after "/Lists/"
string[] temp1 = urlToSearch.Split(new String[] { "/Lists/" }, StringSplitOptions.RemoveEmptyEntries);
if (temp1.Length > 1)
{
// Next, get everything before the first "/"
string[] temp2 = temp1[1].Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
result = temp2[0];
}
然后,你的答复将存放在结果变量中。