Check if string ends with certain pattern
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?
Solution 1:
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.
Solution 2:
You can test if a string ends with work followed by one character like this:
theString.matches(".*work.$");
If the trailing character is optional you can use this:
theString.matches(".*work.?$");
To make sure the last character is a period .
or a slash /
you can use this:
theString.matches(".*work[./]$");
To test for work followed by an optional period or slash you can use this:
theString.matches(".*work[./]?$");
To test for work surrounded by periods or slashes, you could do this:
theString.matches(".*[./]work[./]$");
If the tokens before and after work must match each other, you could do this:
theString.matches(".*([./])work\\1$");
Your exact requirement isn't precisely defined, but I think it would be something like this:
theString.matches(".*work[,./]?$");
In other words:
- zero or more characters
- followed by work
- followed by zero or one
,
.
OR/
- followed by the end of the input
Explanation of various regex items:
. -- any character
* -- zero or more of the preceeding expression
$ -- the end of the line/input
? -- zero or one of the preceeding expression
[./,] -- either a period or a slash or a comma
[abc] -- matches a, b, or c
[abc]* -- zero or more of (a, b, or c)
[abc]? -- zero or one of (a, b, or c)
enclosing a pattern in parentheses is called "grouping"
([abc])blah\\1 -- a, b, or c followed by blah followed by "the first group"
Here's a test harness to play with:
class TestStuff {
public static void main (String[] args) {
String[] testStrings = {
"work.",
"work-",
"workp",
"/foo/work.",
"/bar/work",
"baz/work.",
"baz.funk.work.",
"funk.work",
"jazz/junk/foo/work.",
"funk/punk/work/",
"/funk/foo/bar/work",
"/funk/foo/bar/work/",
".funk.foo.bar.work.",
".funk.foo.bar.work",
"goo/balls/work/",
"goo/balls/work/funk"
};
for (String t : testStrings) {
print("word: " + t + " ---> " + matchesIt(t));
}
}
public static boolean matchesIt(String s) {
return s.matches(".*([./,])work\\1?$");
}
public static void print(Object o) {
String s = (o == null) ? "null" : o.toString();
System.out.println(o);
}
}
Solution 3:
Of course you can use the StringTokenizer
class to split the String with '.' or '/', and check if the last word is "work".
Solution 4:
You can use the substring method:
String aString = "This.is.a.great.place.too.work.";
String aSubstring = "work";
String endString = aString.substring(aString.length() -
(aSubstring.length() + 1),aString.length() - 1);
if ( endString.equals(aSubstring) )
System.out.println("Equal " + aString + " " + aSubstring);
else
System.out.println("NOT equal " + aString + " " + aSubstring);