user3453159
user3453159

Reputation: 159

How to separate the final part of the text in Java?

I have this text for example

xxxyyy zzzzzz.xxxxxxxxy xyxyzzdxy xxxxx.yyyzyzzz.september/17/2014

I want to separate the date (september/17/2014) that is in the end of the text after final "." and then put this date in another line

in the end I want something like this.

xxxyyy zzzzzz.xxxxxxxxy xyxyzzdxy xxxxx.yyyzyzzz.

september/17/2014

Upvotes: 0

Views: 143

Answers (4)

inutza
inutza

Reputation: 5

Have a look at lastIndexOf()

Java strings tutorial

Let's assume your desired text is stored in my_string, then you'd do something like:

int last_idx = my_string.lastIndexOf('.');
String date_substring = my_string.substring(last_idx + 1);

Upvotes: 0

biology.info
biology.info

Reputation: 3569

Or this

String str = "xxxyyy zzzzzz.xxxxxxxxy xyxyzzdxy xxxxx.yyyzyzzz.september/17/2014"

String firstString = str.split("\\.")[0]; //xxxyyy zzzzzz.xxxxxxxxy xyxyzzdxy xxxxx.yyyzyzzz
String secondString = str.split("\\.")[1]; //september/17/2014

Upvotes: 0

Mohan
Mohan

Reputation: 153

Try this

String str = "xxxyyy zzzzzz.xxxxxxxxy xyxyzzdxy xxxxx.yyyzyzzz.september/17/2014";
System.out.println(str.substring(str.lastIndexOf(".") + 1));

Upvotes: 0

parakmiakos
parakmiakos

Reputation: 3020

Try this :

String s = "your.string.date";
String d = s.substring(s.lastIndexOf(".")+1);
String r = s.substring(0,s.lastIndexOf("."));

d will contain the date and r the rest of the string.

Print it however you like :

System.out.print(r + "\n" + d);

Upvotes: 2

Related Questions