Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to get sub string from main string like follows .
My main string is
<iframe width="640" height="360" src="https://www.youtube.com/embed/KVlXccl-XBA" frameborder="0" allowfullscreen></iframe>


I want to get only url from this this like
https://www.youtube.com/embed/KVlXccl-XBA

So please let me know how to find this sub string from my main string.
Posted

You can use Regular Expression for this purpose. Try this

C#
Regex regex = new Regex("src=\"(.*)\"");
 
Share this answer
 
Also here is a solution for you that does not use Regex:
C#
string mainString = "<iframe width=\"640\" height=\"360\" src=\"https://www.youtube.com/embed/KVlXccl-XBA\" frameborder=\"0\" allowfullscreen></iframe>";

int start = mainString.IndexOf("src=\"") + 5;
int count = mainString.Length - mainString.IndexOf("\"", start) - 2;

string subString = mainString.Substring(start, count);
 
Share this answer
 
In addition to regex solutions, if the text you are loading is html you can use the agility pack to parse it

https://htmlagilitypack.codeplex.com/[^]

If the html is valid xml you could even use XmlDocument to parse it.
 
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