且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

剪掉一个字符串的特定部分

更新时间:2022-10-19 13:38:43

一种方法是使用正则表达式。请考虑以下事项:

  string  testString =  < iframe width = \395 \height = \295 \src = \// www.***.com/embed/fdbazqdXBeE\\ \\frameborder = \0 \webkitallowfullscreen mozallowfullscreen allowfullscreen>< / iframe>; 

System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex( src = \(。*?)\);

if (re.IsMatch(testString)){
System.Console.WriteLine(re.Match(testString).Value );
System.Console.WriteLine(re.Match(testString).Value.Substring( 5 )。TrimEnd( ' '));
}



输出为:

 src =// www.***.com/embed/fdbazqdXBeE
//www.***.com/embed/fdbazqdXBeE


If i have the iframe of a video like this:

<iframe width="395" height="295" src="//www.***.com/embed/fdbazqdXBeE" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>


and I want to get the source "src" only, how can I do this using C#.

Thanks in advance.

One way is to use regular expressions. Consider the following:
string testString = "<iframe width=\"395\" height=\"295\" src=\"//www.***.com/embed/fdbazqdXBeE\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";

System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("src=\"(.*?)\"");

if (re.IsMatch(testString)) {
   System.Console.WriteLine(re.Match(testString).Value);
   System.Console.WriteLine(re.Match(testString).Value.Substring(5).TrimEnd('"'));
}


The output is:

src="//www.***.com/embed/fdbazqdXBeE"
//www.***.com/embed/fdbazqdXBeE