Java替换字符串前后的指定字符(类似于String的trim()方法)
想到了替换字符串或者正则表达式,但是没想到怎么实现!
看来平常得多看看源码!!!
2种实现方法,第一种简单但不效率低,不推荐;第二种参考JDK的方法实现,高效,推荐。实现方法如下:
方法1:
用JDK自带的trim方法加replace方法:
String str = "+++hello world+-,nihao!++++";
//传字符
System.out.println(str.replace('+',' ').trim().replace(' ', '+'));
//或传字符串
//System.out.println(str.replace("+"," ").trim().replace(" ", "+"));
方法2:
JDK自带的trim方法:
public String trim() {
int len = value.length;
int st = 0;
char[] val = value;
while ((st < len) && (val[st] <= ' ')) {
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
参考上面JDK自带的trim方法,实现如下:
public static String custom_trim(String str, char c) {
char[] chars = str.toCharArray();
int len = chars.length;
int st = 0;
while ( (st < len) && (chars[st] == c) ){
st ++;
}
while ( (st < len) && (chars[len-1] == c) ){
len --;
}
return (st >0) && (len<chars.length)? str.substring(st, len): str;
}
扩展:
JDK(1.7)的String类中没有ltrim()和rtrim()方法,现在实现如下:
public static String custom_ltrim(String str, char c) {
char[] chars = str.toCharArray();
int len = chars.length;
int st = 0;
while ( (st < len) && (chars[st] == c) ){
st ++;
}
return (st >0)? str.substring(st, len): str;
}
public static String custom_rtrim(String str, char c) {
char[] chars = str.toCharArray();
int len = chars.length;
int st = 0;
while ( (st < len) && (chars[len-1] == c) ){
len --;
}
return (len<chars.length)? str.substring(st, len): str;
}
完整实例如下:
/**
* 自定义字符串修剪方法
* Created by ylchou@foxmail.com on 18/3/10.
*/
public class TrimDemo {
public static void main(String[] args) {
String str = "+++hello world+-,nihao!++++";
System.out.println(str.replace('+',' ').trim().replace(' ', '+'));//hello+world+-,nihao!
System.out.println(str.replace("+"," ").trim().replace(" ", "+"));//hello+world+-,nihao!
System.out.println(custom_trim(str, '+'));//hello world+-,nihao!
System.out.println(custom_ltrim(str, '+'));//hello world+-,nihao!++++
System.out.println(custom_rtrim(str, '+'));//+++hello world+-,nihao!
}
/**
* 去掉指定字符串前面和后面指定的字符
* @param str
* @param c
* @return
*/
public static String custom_trim(String str, char c) {
char[] chars = str.toCharArray();
int len = chars.length;
int st = 0;
while ( (st < len) && (chars[st] == c) ){
st ++;
}
while ( (st < len) && (chars[len-1] == c) ){
len --;
}
return (st >0) && (len<chars.length)? str.substring(st, len): str;
}
/**
* 去掉指定字符串前面指定的字符
* @param str
* @param c
* @return
*/
public static String custom_ltrim(String str, char c) {
char[] chars = str.toCharArray();
int len = chars.length;
int st = 0;
while ( (st < len) && (chars[st] == c) ){
st ++;
}
return (st >0)? str.substring(st, len): str;
}
/**
* 去掉指定字符串后面指定的字符
* @param str
* @param c
* @return
*/
public static String custom_rtrim(String str, char c) {
char[] chars = str.toCharArray();
int len = chars.length;
int st = 0;
while ( (st < len) && (chars[len-1] == c) ){
len --;
}
return (len<chars.length)? str.substring(st, len): str;
}
}
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。