Reputation: 4818
I am trying to trim StringBuilder
from right side, something like this :
private StringBuilder rtrim(StringBuilder str, Character trimStr) {
if (str.charAt(str.length() - 1) == trimStr) {
return str.deleteCharAt(str.length() - 1);
}
return str;
}
The above function works fine with character as trimStr, but I want to pass trimStr as string. Any library for the same(similar to StringUtils)?
Upvotes: 1
Views: 374
Reputation: 60036
You can use replaceAll or just replaceFirst with regex like so :
String str = " some string ";
str = str.replaceAll("\\s*$", "");//output is " some string"
Edit
If you are using StringBuilder you can use :
private static StringBuilder rtrim(StringBuilder str, String trimStr) {
return new StringBuilder(
str.toString().replaceFirst("(" + Pattern.quote(trimStr) + ")+$", "")
);
}
Some inputs and outputs
" some string " + " " ==> " some string"
" some stringaaa" + "a" ==> " some string"
" some stringabab" + "ab" ==> " some string"
For left trim you can just change your regex to be str.toString().replaceFirst("^(" + Pattern.quote(trimStr) + ")+", "")
private static StringBuilder rtrim(StringBuilder str, String trimStr) {
return new StringBuilder(
str.toString().replaceFirst("^(" + Pattern.quote(trimStr) + ")+", "")
);
}
Another Solution without regex
public StringBuilder rTrim(StringBuilder str, String trimStr) {
if (str != null && trimStr != null) {
int trimLen = trimStr.length();
int strLen = str.length();
if (strLen != 0 && trimLen != 0) {
while (strLen != 0 && str.substring(strLen - trimLen, strLen).equals(trimStr)) {
str = str.delete(strLen - trimLen, strLen);
strLen = str.length();
}
}
}
return str;
}
public StringBuilder lTrim(StringBuilder str, String trimStr) {
if (str != null && trimStr != null) {
int len = trimStr.length();
if (str.length() != 0 && len != 0) {
while (str.length() != 0 && str.substring(0, len).equals(trimStr)) {
str = str.delete(0, len);
}
}
}
return str;
}
Upvotes: 4
Reputation: 798
Something like this could work:
private StringBuilder rtrim(StringBuilder str, String trimStr) {
if(str.length() == 0 || trimStr.isEmpty()) {
return str;
}
int lastIndexOf = str.lastIndexOf(trimStr);
if(lastIndexOf >= 0 && lastIndexOf + trimStr.length() == str.length()) {
str.setLength(lastIndexOf);
}
return str;
}
This will remove trimStr
from the end of the provided str
.
Using lastIndexOf
directly on the StringBuilder
avoids converting everything to a string.
If you want to remove all occurrences of trimStr
at the end of str
you can do something like this:
private StringBuilder rtrim(StringBuilder str, String trimStr) {
if(str.length() == 0 || trimStr.isEmpty()) {
return str;
}
int lastIndexOf;
while((lastIndexOf = str.lastIndexOf(trimStr)) >= 0 && lastIndexOf + trimStr.length() == str.length()) {
str.setLength(lastIndexOf);
}
return str;
}
Upvotes: 0
Reputation: 2598
As you have mentioned, you can use Apache StringUtils
library :
private static StringBuilder rTrim(StringBuilder sb, String stripChars) {
return new StringBuilder(StringUtils.stripEnd(sb.toString(), stripChars));
}
private static StringBuilder lTrim(StringBuilder sb, String stripChars) {
return new StringBuilder(StringUtils.stripStart(sb.toString(), stripChars));
}
Upvotes: 0