问题描述
你好,
什么是解析字符串并找出特定单词的最佳方法。
例如,
www.abc.com/public/test/this_is_test.php
现在在上面的网址中,我需要查找test和this_is_test并将其改为一个单词 - test_this_is_test并使用它重定向到另一个网站。
但是网址可能会有所不同,但有类似的模式。所以我不能只搜索this_is_test这个词..
希望它有意义..
谢谢
Hello,
What's a best way to parse a string and find out the particular word.
For instance,
www.abc.com/public/test/this_is_test.php
Now in the above url, I need to find out test and this_is_test and make it into one word - test_this_is_test and use it to redirect to another site.
But the url can vary but with a similar pattern. So I can not just search for the word this_is_test..
Hope it makes sense..
Thanks
推荐答案
string url = "www.abc.com/public/test/this_is_test.php";
string[] splitString = url.Split('/');
var lastValue = string.Empty;
string lastPart = string.Empty;
for(int i = 0; i <splitstring.count;>{
var secondValue = splitString[2].ToString();
lastValue = splitString[3].ToString();
}
现在你将在单独的变量中得到test和this_is_test.php。
再次你需要拆分lastValue使用'。'并获得第一部分如下:
Now you will get the test and this_is_test.php in the separate variables.
Again you need to split the lastValue with the '.' and get the first part as below:
string[] lastSplit = lastValue.Split('.');
lastPart = lastSplit[0].ToString();
所以你的最终值将是secondValue和lastpart的连续符号如下:
So your final value will be the concatantion of the secondValue and lastpart as below:
string finalValue = secondValue + "_" + lastPart;
希望它对您有所帮助。
Hope it will be helpful to you.
这篇关于我如何解析网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!