Debugmode
Debugmode

Reputation: 93

How to split data from string?

How can i split data from string?

I have my string like this,

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.

I want this information to be split like

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.

How can i split above string into these four parts and saving into temp variable for each part?

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

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

string urlTitle= Yahoo! India

string 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.

How can i do that?

Upvotes: 0

Views: 1201

Answers (2)

MattDavey
MattDavey

Reputation: 9017

Assuming that format of your input string will not change (ie the order of the keys), you could try something like this:

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...

Upvotes: 6

frennky
frennky

Reputation: 13934

You can try this:

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

Upvotes: 0

Related Questions