From strings like this
<iframe width="560" height="315" src="https://www.youtube.com/embed/KRFHiBW9RE8" frameborder="0" allowfullscreen></iframe>
I need to select only the source, so the word between src="the string I need"
I have tried using IndexOf the word src=" but the link doesn't have a fixed number of characters to set the ending.
So, the Substring after function reads the URL declared under the name=S and prints the result of the string after the substring. The following code shows how to use after function () to represent the delimiter with the numbers. Following is the next example showing Parenthesis values. The following code will result in the below output.
Finally, use the following PowerShell substring Method to extract the substring after the reference character $subdomain.Substring($refcharacter+1) As expected, the result extracts the substring after the period, which is itechguides.com !
2. RIGHT, LEN, and FIND Functions to Extract Text After a Character 3. Use of LEFT, FIND, and SUBSTITUTE Functions to Excerpt Text After a Character 4. Using RIGHT, SEARCH, and SUBSTITUTE Functions to Extract Specific Characters 5. Using RIGHT, SUBSTITUTE, and REPT Functions to Extract Text After a Character 6.
The substring function helps in returning an atomic value not simple a node. The above function takes two inputs where the first string does search and the second string is the string to be searched for the first string.
If you're trying to parse some HTML code - it might be better to use HTMLAgilityPack.
But in the case it is just some set of strings you've obtained from somewhere and want to parse - you can also do it using regular expressions:
string s ="<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen></iframe>";
var match = Regex.Match(s, "src=\"(.*?)\"");
string src;
if (match.Success)
src = match.Groups[1].Value;
A naive implementation, in which I assume you have a string as input:
string input = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen></iframe>";
if (input.Contains("src=\""))
{
string output = input.Substring(input.IndexOf("src=\"") + 5);
// output is: https://www.youtube.com/embed/KRFHiBW9RE8" frameborder="0" allowfullscreen></iframe>
output = output.Substring(0, output.IndexOf("\""));
// output is: https://www.youtube.com/embed/KRFHiBW9RE8
}
It will certainly miss edge cases like src ="
, but will give you a place to start. Obviously this is also a problem which can be solved using regular expressions; I'll leave that for someone else to answer.
I'd be tempted to split all the properties out into an array as it's possible I might want some of the others later on as well. In doing this it would also allow easy access to the 'src' property. So I'd do something like this:
string iFrameString = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/KRFHiBW9RE8\" frameborder=\"0\" allowfullscreen>";
//split properties based on spaces
string[] tagProps = iFrameString.Split(new Char[]{' '});
//get the property out.
string prop = "src=\"";
string source = Array.Find(tagProps, x => x.StartsWith(prop, StringComparison.InvariantCultureIgnoreCase));
string ModifiedSource = source.Substring(prop.Length,source.Length - prop.Length);
The benefit of this is that you have all the other properties in your array as well and you could get those out if needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With