如何从字符串分割数据

本文关键字:分割 数据 字符串 | 更新日期: 2023-09-27 18:14:22

如何从字符串分割数据?

我的字符串是这样的

Url=http://www.yahoo.com UrlImage=http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png UrlTitle=Yahoo! India UrlDescription=Welcome to Yahoo!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and information.

我想把这个信息分割成

http://www.yahoo.com
http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png
Yahoo! India
Welcome to Yahoo!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and information.

我如何将上面的字符串分成这四个部分并保存为每个部分的临时变量?

string url= http://www.yahoo.com

string urlImage= http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png

string urlTitle= Yahoo!印度

string urlDescription=欢迎访问Yahoo!,是全球访问量最大的主页。快速找到你正在寻找的东西,与朋友保持联系,并随时了解最新的新闻和信息。

我该怎么做呢?

如何从字符串分割数据

假设输入字符串的格式不会改变(即键的顺序),您可以尝试这样做:

var input = "Url:http://www.yahoo.com UrlImage:http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png UrlTitle:Yahoo! India UrlDescription:Welcome to Yahoo!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and information."
// Convert the input string into a format which is easier to split...
input = input.Replace("Url=", "")
             .Replace("UrlImage=", "|")
             .Replace("UrlTitle=", "|")
             .Replace("UrlDescription=", "|");
var splits = input.Split("|");
string url         = splits[0]; // = http://www.yahoo.com
string image       = splits[1]; // = http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png
string title       = splits[2]; // = Yahoo! India
string description = splits[3]; // = Welcome to Yahoo!, the world's...

你可以试试:

var input = "Url:http://www.yahoo.com UrlImage:http://l.yimg.com/a/i/ww/met/yahoo_logo_in_061509.png UrlTitle:Yahoo! India UrlDescription:Welcome to Yahoo!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and information.";
var result = input.Split(new []{"Url:","UrlImage:","UrlTitle:","UrlDescription:"}, StringSplitOptions.RemoveEmptyEntries);