Reputation: 159
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
Reputation: 5
Have a look at lastIndexOf()
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
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
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
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