C# 如何从字符串中分割数据?

C# 如何从字符串中分割数据?,c#,string,split,C#,String,Split,如何从字符串中分割数据 我有这样的绳子 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 tou

如何从字符串中分割数据

我有这样的绳子

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.
如何将上述字符串拆分为这四个部分,并将每个部分保存到temp变量中

字符串url=

字符串urlImage=

字符串urlttitle=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."

// 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);
您可以尝试以下方法:

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

您可能需要自己为它编写解析器。您是如何定义字符串格式的?“Key:Value”值在下一个可以解析为键的块之后终止?如何对包含冒号的值进行编码?是否总是有4个部分?您可以使用String.IndexOf(“”)或LINQ SkipWhile/TakeWhile/Take/Skip…您能提供/引用一个例子吗?您的输入字符串有一个大问题。您正在使用“:”作为键/值delimeter,但一个或多个数据点也包含“:”字符。这将使解析这个变得更加困难。您可以尝试转义数据中的问题字符,或将键/值分隔符更改为另一个字符。@MattDavey:我已将其更改为=from:。希望一切顺利。现在我如何分割它呢?您可能需要自己为它编写一个解析器。您是如何定义字符串格式的?“Key:Value”值在下一个可以解析为键的块之后终止?如何对包含冒号的值进行编码?是否总是有4个部分?您可以使用String.IndexOf(“”)或LINQ SkipWhile/TakeWhile/Take/Skip…您能提供/引用一个例子吗?您的输入字符串有一个大问题。您正在使用“:”作为键/值delimeter,但一个或多个数据点也包含“:”字符。这将使解析这个变得更加困难。您可以尝试转义数据中的问题字符,或将键/值分隔符更改为另一个字符。@MattDavey:我已将其更改为=from:。希望一切顺利。现在我怎么才能把它分开呢?