Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
David Lee 145 29-Aug-14 6:50am    
See the below page. Uri class may help you.
http://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx

Hai Andy,
You cna use the string array and the break this URL with the '/' separator as below:
C#
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();
}

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:
C#
string[] lastSplit = lastValue.Split('.');
lastPart = lastSplit[0].ToString();

So your final value will be the concatantion of the secondValue and lastpart as below:
C#
string finalValue = secondValue + "_" + lastPart;

Hope it will be helpful to you.
 
Share this answer
 
Isn't this approaching close to mod_rewrite[^] territory if working on non-Windows server? That could be worth looking into if so as it can save you time with having to code out the rest of your redirect.

If that isn't what you want to do (or what you are able to do) then once you have done the above 'conversion' you could do the following: If you set up a database table with at least two columns (urls to match and redirect urls) then you can perform a select query on that database table to search for "test_this_is_test" and have it return the url that you want it to redirect to which would be in the second column of the same row.

Once you have the url that you want to redirect the user to you can use a simple 'location' call pointing to the url that was pulled from the database.

Since we do not know how many url's or parsable url's you intend to be used in your system it is hard to say how easily you can do this. You would have to have a database row for each and every url that could possibly be passed to the script and where to where it should redirect.

You can use mod_rewrite to 'forward' to external sites. You would just have to write the mod_rewrite "rules" for how it should match urls. But if that doesn't work for you then perhaps my suggestion above will work okay for you after performing the previously mentioned trick to get the "test_this_is_test". Best of luck!
 
Share this answer
 
Check out Uri class[^] in .NET

Uri class provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.
There is many methods that can help in getting the right information from an URL string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900