techiee
techiee

Reputation: 96

Parsing and splitting a String

I want to format one string "Event"

When I print it. it gives me result like :

When : Wed 18 Apr 2012 04:30 to Wed 18 Apr 2012 06:30  

Who: [email protected]

Event status : Confirmed.

It gives answers in this 3 category.

Who,when and Event Status.

But i want these three results in a separate string .How to format these string so that i ll get it seperately

Upvotes: 1

Views: 230

Answers (4)

vulkanino
vulkanino

Reputation: 9124

If I understand well, you have as input the string:

When : Wed 18 Apr 2012 04:30 to Wed 18 Apr 2012 06:30  

Who: [email protected]

Event status : Confirmed.

And you want to get as output three strings (or an array of strings) containing respectively:

whenString="Wed 18 Apr 2012 04:30 to Wed 18 Apr 2012 06:30"
whoString="[email protected]"
eventString="Confirmed."

If this is the case, then

String[] lines = source.split("\n+");

int index = lines[0].indexOf("When"); 
String whenString = lines[0].substring(index+7);

index = lines[1].indexOf("Who"); 
String whoString = lines[1].substring(index+5);

index = lines[2].indexOf("Event status"); 
String whoString = lines[2].substring(index+15);

Upvotes: 0

Siva Charan
Siva Charan

Reputation: 18064

Split the content based on \r\n, which treats as new line.

Upvotes: 0

S.P.
S.P.

Reputation: 3054

If there are single new line characters in between the 3 lines then use this :

String splitLine[] = String.split("\\r?\\n");

Upvotes: 0

shift66
shift66

Reputation: 11958

String[] parts = resultString.split("\n+");

This will split your string by enters (\n+ means one or more new line symbol) and store parts in parts array

Upvotes: 4

Related Questions