Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
2.71/5 (3 votes)
See more:
I have a string of URLs and i need to break the urls using "http" as index.Below is my string example.
string X= http://www.yachtbroker.dk/_publicPics/Boats443B.JPGhttp://www.yachtbroker.dk/_publicPics/Boats/543B.gifhttp://yachtbrokerdk/_publicPics/Boats/idue.jpeg

how to achieve it?
Posted

There are a couple of ways:
C#
string[] urls = inputString.Split(new string[] {"http://"}, StringSplitOptions.RemoveEmptyEntries);
Is the simplest, but you will need to add the "http://" part back onto each string if you need it later.

The alternative is to use a Regex, which is a little more complex, but could preserve the complete URL.
 
Share this answer
 
Comments
ahmed ali mohd 30-Nov-13 5:42am    
thanx im getting the seperate strings but http:// is missing in the urls..how to resolve?
OriginalGriff 30-Nov-13 5:59am    
That's what I said - with String.Split you lose the separator so you would have to add them back when you use them.
Or, use a Regex, but that is more work.
One way to add them back would be to use Linq:
string[] urls = inputString.Split(new string[] {"http://"}, StringSplitOptions.RemoveEmptyEntries);
urls = urls.Select(s => "http://" + s).ToArray();
Hi Ahmed

user REGEX to resolve it..


try like this..

C#
string url = "http://www.yachtbroker.dk/_pblicPics/Boats443B.JPGhttp://www.yachtbroker.dk/_publicPics/Boats/543B.gifhttp://yachtbrokerdk/_publicPics/Boats/idue.jpeg";
             string[] urls = Regex.Split(url, @"http://");




to remove the empty string in the array.
use this..

C#
string[] urls = Regex.Split(url, @"http://").Where(k => !string.IsNullOrWhiteSpace(k)).ToArray();



try this code...


C#
string url = "http://www.yachtbroker.dk/_publicPics/Boats443B.JPGhttp://www.yachtbroker.dk/_publicPics/Boats/543B.gifhttp://yachtbrokerdk/_publicPics/Boats/idue.jpeg";

              string token = "http://";
              var urls = url.Split(new string[] { token }, StringSplitOptions.RemoveEmptyEntries).Select(k => token + k).ToArray();
 
Share this answer
 
v3
Comments
ahmed ali mohd 30-Nov-13 5:49am    
Thanx for replies But I want to include http:// in my substrings urls..how to resolve?
ahmed ali mohd 30-Nov-13 5:52am    
want answerv as below
http://www.yachtbroker.dk/_pblicPics/Boats443B.JPG
http://www.yachtbroker.dk/_pblicPics/Boats443B.gif
http://www.yachtbroker.dk/_pblicPics/Boats443B.jpeg
Karthik_Mahalingam 30-Nov-13 6:31am    
try my updated solution..
try below code
string X = "http://www.yachtbroker.dk/_publicPics/Boats443B.JPGhttp://www.yachtbroker.dk/_publicPics/Boats/543B.gifhttp://yachtbrokerdk/_publicPics/Boats/idue.jpeg";
 
string[] values = X.Split(new string[] { "http" }, StringSplitOptions.None);
 
Share this answer
 
hi please check with follwoing my solution


C#
string originalString="http://www.yachtbroker.dk/_publicPics/Boats443B.JPGhttp://www.yachtbroker.dk/_publicPics/Boats/543B.gifhttp://yachtbrokerdk/_publicPics/Boats/idue.jpeg";
           String[] spltOriginal = originalString.Split(new string[] { "http:" }, StringSplitOptions.None);
           for (int i = 1; i < spltOriginal.Length; i++)
           {

               Console.WriteLine("http:" + spltOriginal[i]);
           }
           Console.ReadLine();


Hope This Helps!!!

Dont forget to mark as answer if it works for and also up vote it thanks.
 
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