剪掉一个字符串的特定部分 [英] cut specific part of a string

查看:77
本文介绍了剪掉一个字符串的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的视频的iframe:

 <   iframe     width   =  395    height   =  295    src   =  // www.youtube.com/embed/fdbazqdXBeE     frameborder   = < span class =code-keyword> 0    webkitallowfullscreen     mozallowfullscreen     allowfullscreen  >   <   / iframe  >  



我只想获得源src,我怎么能用C#来做这件事。



提前谢谢。

解决方案

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

  string  testString =  < iframe width = \395 \height = \295 \src = \// www.youtube.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.youtube.com/embed/fdbazqdXBeE
//www.youtube.com/embed/fdbazqdXBeE


If i have the iframe of a video like this:

<iframe width="395" height="295" src="//www.youtube.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.youtube.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.youtube.com/embed/fdbazqdXBeE"
//www.youtube.com/embed/fdbazqdXBeE


这篇关于剪掉一个字符串的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆