Match first If the String is ending with your options or not like below -
if (value.endsWith("\'") || value.endsWith("--") || value.endsWith("-"))
and so on your options.
and then remove last character from the String in this if only like this -
value = value.substring(0, value.length()-1);
check below sample code to understand better -
String value = "!lovely?! hi!?'";
if (value.endsWith("\'") || value.endsWith("--") || value.endsWith("-")) {
value = value.substring(0, value.length()-1);
}
System.out.println(value);
If you want to use Java Regex for comparison then use in this manner -
if(value.matches(".*[' -- - ly ]$"))
- .* for starts with more than one characters.
- [' -- - ly] gives you actual comparison of your options. provide your options with spaces between each character.
- $ for String ends with any of them.
In this manner your can use Regex for comparison of String.
More specifically you can use this best way if you want to remove last one,two,three and four characters from the String.
//provide only one character String that you want to check and remove
if (value.matches(".*[' - ?]$")){
value = value.substring(0, value.length()-1);
}
//provide two character String that you want to check and remove
if (value.matches(".*[-- ly ab cd ef]$")){
value = value.substring(0, value.length()-2);
}
//provide three character String that you want to check and remove
if (value.matches(".*[-- lyy abc def ghi]$")){
value = value.substring(0, value.length()-3);
}
//provide four character String that you want to check and remove
if (value.matches(".*[-- lyyy abcd efgh ijkl]$")){
value = value.substring(0, value.length()-4);
}