- 相關(guān)推薦
sun認(rèn)證java關(guān)于字符串處理技巧
在sun認(rèn)證考試中,java語言關(guān)于字符串處理技巧有哪些呢?下面yjbys小編跟大家分享字符串的一些處理方法如下:
* @版本 1.0
*/
public class StringUtil {
public StringUtil() {
}
/**
* @description 字符串替代函數(shù)
* @param source String 源字符串
* @param be_rep_str String 需要被替換的子串
* @param rep_str String 要替換的目標(biāo)串
* @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進(jìn)行坼分。
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"是轉(zhuǎn)換碼距
// 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;
}
/**
* 全角 轉(zhuǎn)換成 -> 半角
* @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("半角轉(zhuǎn)換成全角:" + bj + " ——> " + BQchange(bj));
String s = " ";
String qs = " ";
System.out.println("全角轉(zhuǎn)換成半角:"+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);
}
}
【sun認(rèn)證java字符串處理技巧】相關(guān)文章:
SUN認(rèn)證JAVA程序員簡(jiǎn)介10-25
Sun國(guó)際認(rèn)證09-04
sun認(rèn)證:java程序設(shè)計(jì)考試試題08-20
SUN認(rèn)證考試科目06-03
sun認(rèn)證報(bào)考指南08-27
SUN認(rèn)證考試流程06-10