1、将字符串中大写字符转为小写字母,并同时在转换的字符前增加符号“_”
public static String splitStringWhitUpper(final String target) {
final StringBuilder sb = new StringBuilder((int) (target.length() * 1.3));
char c;
for (int i = 0; i < target.length(); i++) {
if (((c = target.charAt(i)) >= 'A') && (c <= 'Z')) {
if (sb.length() != 0) {
sb.append('_');
}
sb.append((char) (c + 32));
} else {
sb.append(c);
}
}
return sb.toString();
}
2、类似String.split()的操作,只是执行效率上比前会者快一点
public static String[] split(final String str, final String splitFlag) {
String[] ss = new String[str.length() / 4];
// start index
int si = 0;
// end index
int ei = 0;
int ind = 0;
while (true) {
ei = str.indexOf(splitFlag, si);
if (ei == -1) {
// 已经结束
break;
}
// 不过滤掉零长字符
ss[ind++] = str.substring(si, ei);
si = ei + splitFlag.length();
if (ind == ss.length) {
String[] nss = new String[(int) (ss.length * 1.4)];
System.arraycopy(ss, 0, nss, 0, ss.length);
ss = nss;
}
}
// 增加最后一段
ss[ind++] = str.substring(si);
if (ss.length == ind) {
return ss;
} else {
final String[] bs = new String[ind];
System.arraycopy(ss, 0, bs, 0, ind);
return bs;
}
}
3、根据间隔符,得到目标位置内容
public static String splitInd(final String str, final String splitFlag, final int index) {
// start index
int si = 0;
// end index
int ei = 0;
// 当前索引位置
int ind = 0;
while (true) {
ei = str.indexOf(splitFlag, si);
if (ei == -1) {
// 已经结束
break;
}
if (ind == index) {
return str.substring(si, ei);
}
si = ei + splitFlag.length();
ind++;
}
if (ind == index) {
// 最后一个的情况
return str.substring(si);
}
// 不存在目标位置的字串
return null;
}