* bael-2361

* Update RemoveLeadingAndTrailingZeroes.java

* moving examples to java-string project
This commit is contained in:
fanatixan
2018-12-02 19:36:38 +01:00
committed by maibin
parent 0a87548bf8
commit 857192358c
2 changed files with 246 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
package com.baeldung.string.removeleadingtrailingchar;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.CharMatcher;
public class RemoveLeadingAndTrailingZeroes {
public static String removeLeadingZeroesWithStringBuilder(String s) {
StringBuilder sb = new StringBuilder(s);
while (sb.length() > 1 && sb.charAt(0) == '0') {
sb.deleteCharAt(0);
}
return sb.toString();
}
public static String removeTrailingZeroesWithStringBuilder(String s) {
StringBuilder sb = new StringBuilder(s);
while (sb.length() > 1 && sb.charAt(sb.length() - 1) == '0') {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
public static String removeLeadingZeroesWithSubstring(String s) {
int index = 0;
for (; index < s.length() - 1; index++) {
if (s.charAt(index) != '0') {
break;
}
}
return s.substring(index);
}
public static String removeTrailingZeroesWithSubstring(String s) {
int index = s.length() - 1;
for (; index > 0; index--) {
if (s.charAt(index) != '0') {
break;
}
}
return s.substring(0, index + 1);
}
public static String removeLeadingZeroesWithApacheCommonsStripStart(String s) {
String stripped = StringUtils.stripStart(s, "0");
if (stripped.isEmpty() && !s.isEmpty()) {
return "0";
}
return stripped;
}
public static String removeTrailingZeroesWithApacheCommonsStripEnd(String s) {
String stripped = StringUtils.stripEnd(s, "0");
if (stripped.isEmpty() && !s.isEmpty()) {
return "0";
}
return stripped;
}
public static String removeLeadingZeroesWithGuavaTrimLeadingFrom(String s) {
String stripped = CharMatcher.is('0')
.trimLeadingFrom(s);
if (stripped.isEmpty() && !s.isEmpty()) {
return "0";
}
return stripped;
}
public static String removeTrailingZeroesWithGuavaTrimTrailingFrom(String s) {
String stripped = CharMatcher.is('0')
.trimTrailingFrom(s);
if (stripped.isEmpty() && !s.isEmpty()) {
return "0";
}
return stripped;
}
public static String removeLeadingZeroesWithRegex(String s) {
return s.replaceAll("^0+(?!$)", "");
}
public static String removeTrailingZeroesWithRegex(String s) {
return s.replaceAll("(?!^)0+$", "");
}
}