如何从主要大字符串中获取子字符串? [英] How to Get sub string from main big string ?

查看:103
本文介绍了如何从主要大字符串中获取子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想从主字符串中获取子字符串,如下所示。

我的主要字符串是

< iframe width =640height =360src =https://www.youtube.com/embed/KVlXccl-XBAframeborder =0allowfullscreen>< / iframe> 





我想从这里得到这个唯一的网址就像

https://www.youtube.com/embed/KVlXccl-XBA



所以请让我知道如何从我的主字符串中找到这个子字符串。

解决方案

您可以使用正则表达式来实现此目的。试试这个



正则表达式正则表达式= 正则表达式(  src = \(。*)\); 


这里有一个解决方案,你不使用正则表达式

  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);


< blockquote>除了正则表达式解决方案,如果您加载的文本是html,您可以使用敏捷包来解析它



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



如果html是有效的xml,你甚至可以使用XmlDocument来解析它。


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.

解决方案

You can use Regular Expression for this purpose. Try this

Regex regex = new Regex("src=\"(.*)\"");


Also here is a solution for you that does not use Regex:

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);


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.


这篇关于如何从主要大字符串中获取子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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