這是本人與同事在工作中對字符串處理的總結,發表出來,希望對大家有些幫助,也希望大家給予指正,謝謝!~
import java.util.*;
/** *
文件標題:
*關於字符串的一些處理方法:
* @版本 1.0
*/
public class StringUtil {
public StringUtil() {
}
/**
* @description 字符串替代函數
* @param source String 源字符串
* @param be_rep_str String 需要被替換的子串
* @param rep_str String 要替換的目標串
* @param return_str String
* @return String
*/
public static String replaceStr(String source, String be_rep_str,
String rep_str, String return_str) {
if (source.indexOf(be_rep_str) < 0) {
return source;
}
else {
String head = source.substring(0, source.indexOf(be_rep_str));
String body = rep_str +
source.substring(source.indexOf(be_rep_str) + be_rep_str.length());
return_str = replaceStr(head + body, be_rep_str, rep_str,
return_str);
}
return return_str;
}
public static String[] getFields(String test, String sep) { //坼分字符,將字句test按照sep進行坼分。
sep = "\u0020";
if (test.indexOf(sep) != -1) {
if ( (test.indexOf(sep) > test.indexOf("#")) &&
test.indexOf("#") != -1) {
sep = "#";
}
}
else {
sep = "#";
}
String[] st;
List flds = new ArrayList();
if (sep.equals("\u0020")) {
StringTokenizer stto = new StringTokenizer(test.trim(), sep);
while (stto.hasMoreTokens()) {
flds.add(stto.nextToken());
}
st = (String[]) flds.toArray(new String[stto.countTokens()]);
}
else {
st = test.trim().split(sep);
}
return st;
}
// public String DBC2SBC(String str)
// {
// int i ;
// String result = "";
// for (i = 0 ; i < str.length(); i++)
// {
// code = str.charCodeAt(i) ;
// // “65281”是“!”,“65373”是“}”
// if (code >= 65281 && code < 65373)
// // “65248”是轉換碼距
// result += String.fromCharCode(str.charCodeAt(i) - 65248) ;
// else
// result += str.charAt(i) ;
// }
// return result ;
// }
/**
* 半角 DBC case -> 全角 SBC case
* @param QJstr String
* @return String
*/
public static final String BQchange(String QJstr) {
String outStr = "";
String Tstr = "";
byte[] b = null;
for (int i = 0; i < QJstr.length(); i++) {
try {
Tstr = QJstr.substring(i, i + 1);
b = Tstr.getBytes("unicode");
}
catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b[3] != -1) {
b[2] = (byte) (b[2] - 32);
b[3] = -1;
try {
outStr = outStr + new String(b, "unicode");
}
catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
}
else {
outStr = outStr + Tstr;
}
}
return outStr;
}
/**
* 全角 轉換成 -> 半角
* @param QJstr String
* @return String
*/
public static final String QBchange(String QJstr) {
String outStr = "";
String Tstr = "";
byte[] b = null;
for (int i = 0; i < QJstr.length(); i++) {
try {
Tstr = QJstr.substring(i, i + 1);
b = Tstr.getBytes("unicode");
}
catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b[3] == -1) {
b[2] = (byte) (b[2] + 32);
b[3] = 0;
try {
outStr = outStr + new String(b, "unicode");
}
catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
}
else {
outStr = outStr + Tstr;
}
}
return outStr;
}
public static void main(String aa[]) {
String qj = "#?¥%……?*()";
String bj = "#@$%^&*()";
System.out.println("半角轉換成全角:" + bj + " --> " + BQchange(bj));
String s = " ";
String qs = " ";
System.out.println("全角轉換成半角:"+qj + " -->" + QBchange(qj));
System.out.println( (int) (QBchange(qs).toCharArray()[0]));
System.out.println( (int) s.toCharArray()[0]);
String dd[] = getFields("23#99#99#99", "#");
for (int i = 0; i < dd.length; i++) {
System.out.println("asfd" + dd[i]);
}
System.out.println(dd.length);
}
}