Reputation: 55
I want to replace a string by removing the s in the end
Example
Sticks -> Stick
STiCKs -> STiCK
StICks -> StICK
sticks -> stick
while using the
string.replace("sticks", "stick");
doesn't maintain case as it is case sensitive, so I'm seeking for a better option.
Upvotes: 2
Views: 623
Reputation: 25933
I don't really get why all the answers so far are so complex. You can just check the last character and if it's a s
(or S
) you use String#substring
(documentation) and leave out the last character:
String text = "STiCks";
char lastCharacter = text.charAt(text.length() - 1);
if (lastCharacter == 'S' || lastCharacter == 's') {
text = text.substring(0, text.length() - 1);
}
If you want to apply that method to multiple words, for example in a sentence, tokenize the sentence first. Then apply the method to each word and rebuild the sentence.
String sentence = StiCks are nice, I like sticks"
String[] words = sentence.split(" ");
StringJoiner joiner = new StringJoiner(" ");
for (String word : words) {
joiner.add(removePluralSuffix(word));
}
String result = joiner.toString();
or the same with Streams:
String result = Arrays.stream(sentence.split(" "))
.map(this::removePluralSuffix)
.collect(Collectors.joining(" "));
Upvotes: 0
Reputation: 37
If you just need to remove the 's'
at the end of the String
you can simply use substring
method like this:
String myString = "sTiCks";
myString = myString.substring(0, myString.length()-1);
// Result "sTiCk"
If you need to remove a char
or String
from your String
not knowing where this part will be, you can try something this:
String myString = "sTiCks";
// Part you want to delete
String stringToDelete = "Ck";
// Find where that part starts inside your String
int index = myString.indexOf(stringToDelete);
// If found, use substring method to take only what is before and after that part
if (index >= 0)
myString = myString.substring(0, index) + myString.substring(index + stringToDelete.length(), myString.length());
// Result "sTis"
This will delete the desired part only the first time it finds it. But if the part you want to delete appears more than once in your String
you can modify the code to this:
String myString = "sTiCks";
// Part you want to delete
String stringToDelete = "s";
int index;
while ((index = myString.indexOf(stringToDelete)) >= 0)
myString = myString.substring(0, index) + myString.substring(index + stringToDelete.length(), myString.length());
// Result "TiCk"
I hope one of these solutions fits your case.
Upvotes: 0
Reputation: 21995
You could use a very simple regex for this mission.
(?i)
guarantees that your regex will be treated case insensitive
string.replaceAll("(?i)(stick)s", "$1");
Upvotes: 4
Reputation: 14872
One possible solution is regular expressions:
StringBuffer sb = new StringBuffer();
Matcher matcher = Pattern.compile("(stick)s", Pattern.CASE_INSENSITIVE) .matcher(inputString);
while(matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1));
}
matcher.appendTail(sb);
String outputString = sb.toString();
Edit: this is more or less what does String::replaceAll, but replaceAll doesn't give a case insensitive option.
Upvotes: 0