分类:
2008-10-16 19:19:25
* @版本 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);
}
}