Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have some values like

1)https://www.facebook.com/pages/Delhi/Shemrock-Chain-of-Preschools/359534360364
2)http://twitter.com/#!/ShemrockGroup
3)http://www.youtube.com/user/shemrocknurseryrhyme

can i split facebook from first string, twitter from second,youtube from thiird string ..

The problem is that what ever may the url i want to split just Domainname part only..
It is fro handling FoloOn part for a Service
Can anybody help me...
Thank you in Advance
Posted
Comments
Sergey Alexandrovich Kryukov 9-May-12 1:25am    
What do you mean by "split facebook", etc? What prevented you from just looking at the string class just to see what members it provides?
--SA
reogeo2008 9-May-12 1:34am    
what i meant is ,if the url is
https://www.facebook.com/pages/Delhi/Shemrock-Chain-of-Preschools/359534360364
just pick the facebook
if http://twitter.com/#!/ShemrockGroup
just pick the twitter

and so on for any url
Sergey Alexandrovich Kryukov 9-May-12 1:26am    
C# or JavaScript? Server or client side?
--SA
reogeo2008 9-May-12 1:29am    
Server side ,C#...i got some functions to make it which return facebook.com as answer but i need just facebook or twitter or youtube etc as answer after substring function worked...
pls give me a suggestion

I think the Uri class can be used to extract the Host name from the url string, it can be split up with '.' and the first word which does not match the exempted strings can be taken as shown below
C#
string[] urls = {@"https://WWW.facebook.com/pages/Delhi/Shemrock-Chain-of-Preschools/359534360364",
        @"http://twitter.com/#!/ShemrockGroup",
        @"http://www.youtube.com/user/shemrocknurseryrhyme"};

foreach(string url in urls)
//"www mail"  etc. to exclude the intial parts not required.
    Console.WriteLine (((new Uri(url)).Host).Split('.')
        .FirstOrDefault(str => !@"www mail".Contains(str.ToLower())) ?? string.Empty);

//Output
//
//facebook
//twitter
//youtube
 
Share this answer
 
Hi
below answer may be helpful to you
JavaScript
<script type="javascript">
---

var nPage = window.location.pathname;
nPage = nPage.substring(nPage.lastIndexOf('/') + 1);
alert(npage);

you can change above substring as per convineint...</script>
 
Share this answer
 
v2
Very dirty, but working solution to get you an idea (I don't recommend you implementing it that way!!!):
C#
private void Form1_Load(object sender, EventArgs e)
{
   string s1 = "https://www.facebook.com/pages/Delhi/Shemrock-Chain-of-Preschools/359534360364";
   string s2 = "http://twitter.com/#!/ShemrockGroup";
   string s3 = "http://www.youtube.com/user/shemrocknurseryrhyme";
   MessageBox.Show(ExtractName(s1));
   MessageBox.Show(ExtractName(s2));
   MessageBox.Show(ExtractName(s3));
}

private static string ExtractName(string s1)
{
   string s1Split = s1.Substring(s1.IndexOf("//") + 2);
   s1Split = s1Split.Remove(s1Split.IndexOf('/'));
   if (s1Split.Contains("www."))
      s1Split = s1Split.Replace("www.", "");
   s1Split = s1Split.Substring(0, s1Split.IndexOf('.'));
   return s1Split;
}
 
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