Say I have a string like this in java:
"this is {my string: } ok"
Note, there can be any number of white spaces in between the various characters. How do I check the above string to see if it contains just the substring:
"{my string: }"
Many thanks!
Put it all into a string variable, say s, then do s.contains("{my string: }); this will return true if {my string: } is in s.
The easiest thing to do is to strip all the spaces from both strings.
If you are looking to see if a String contains another specific sequence of characters then you could do something like this :
You could also use matches. For a decent explanation on matching Strings I would advise you check out the Java Oracle tutorials for Regular Expressions at :
http://docs.oracle.com/javase/tutorial/essential/regex/index.html
Cheers,
Jamie
For this purpose you need to use
String#contains(CharSequence)
.For this purpose
String#trim()
method is used to returns a copy of the string, with leading and trailing whitespace omitted.For e.g.:
Look for the regex
This matches any sequence that contains
Where 'space' here means any whitespace (tab, space, newline, cr)
If you have any number of white space between each character of your matching string, I think you are better off removing all white spaces from the string you are trying to match before the search. I.e. :