If I have a string like:
This.is.a.great.place.too.work.
or:
This/is/a/great/place/too/work/
than my program should give me that the sentence is valid and it has "work".
If I Have :
This.is.a.great.place.too.work.hahahha
or:
This/is/a/great/place/too/work/hahahah
then my program should not give me that there is a "work" in the sentence.
So I am looking at java strings to find a word at the end of the sentence having .
or ,
or /
before it. How can I achieve this?
This is really simple, the String
object has an endsWith
method.
From your question it seems like you want either /
, ,
or .
as the delimiter set.
So:
String str = "This.is.a.great.place.to.work.";
if (str.endsWith(".work.") || str.endsWith("/work/") || str.endsWith(",work,"))
// ...
You can also do this with the matches
method and a fairly simple regex:
if (str.matches(".*([.,/])work\\1$"))
Using the character class [.,/]
specifying either a period, a slash, or a comma, and a backreference, \1
that matches whichever of the alternates were found, if any.