I've tried searching for "Java check if string contains values from array", and other variations on that, but have not come up with anything so hoping someone can help me. I've got the following code that I would like to clean up:
if (dateint.contains("th")){
dateint=dateint.substring(0, dateint.length()-2);
} else if (dateint.contains("st")){
dateint=dateint.substring(0, dateint.length()-2);
} else if (dateint.contains("nd")){
dateint=dateint.substring(0, dateint.length()-2);
} else if (dateint.contains("rd")){
dateint=dateint.substring(0, dateint.length()-2);
}
I'm wondering if I can do something like the following (not true code, but thinking in code):
String[] ordinals = {"th", "st", "nd", "rd"};
if dateint.contains(ordinals) {
dateint=dateint.substring(0, dateint.length()-2);
}
basically checking if any of the values in the array are in my string. I'm trying to use the least amount of code reuqired so that it looks cleaner than that ugly if/else block, and without using a for/next loop..which may be my only option.