I have the following if statement:
String newStr4 = strr.split("2012")[0]; if (newStr4.startsWith("Mon")) { str4.add(newStr4); }
I want it to include startsWith
Mon
Tues
Weds
Thurs
Friday
etc. Is there a simple way to this when using strings? I tried ||
but it didn't work.
To check if a String str starts with a specific prefix string prefix in Java, use String. startsWith() method. Call startsWith() method on the string str and pass the prefix string prefix as argument. If str starts with the prefix string prefix , then startsWith() method returns true.
C++ std::string Checking if a string is a prefix of another In C++14, this is easily done by std::mismatch which returns the first mismatching pair from two ranges: std::string prefix = "foo"; std::string string = "foobar"; bool isPrefix = std::mismatch(prefix. begin(), prefix.
A prefix of a string S is any leading contiguous part of S. A suffix of the string S is any trailing contiguous part of S. For example, "c" and "cod" are prefixes, and "ty" and "ity" are suffixes of the string "codility".
The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False .
Do you mean this:
if (newStr4.startsWith("Mon") || newStr4.startsWith("Tues") || ...)
Or you could use regular expression:
if (newStr4.matches("(Mon|Tues|Wed|Thurs|Fri).*"))
Besides the solutions presented already, you could use the Apache Commons Lang library:
if(StringUtils.startsWithAny(newStr4, new String[] {"Mon","Tues",...})) { //whatever }
Update: the introduction of varargs at some point makes the call simpler now:
StringUtils.startsWithAny(newStr4, "Mon", "Tues",...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With