String re = "";
if (str != null) {
re = str.replaceAll(" ", " ").replaceAll("/n", "
");
}
return re;
}
/**
* 中文转unicode
* @param str
* @return 反回unicode编码
*/
public static String chinaToUnicode(String str)
{
String result ="";
for (int i = 0; i < str.length(); i++)
{
int chr1 = (char) str.charAt(i);
result += "//u" + Integer.toHexString(chr1);
}
return result;
}
/**
* unicode转中文
* @param str
* @return 中文
*/
public static String unicodeToChinese(String str)
{
String re="";
for(char c : str.toCharArray()){
re=re+c;
//System.out.print(c);
}
return re;
}
/**/// /
// / 转全角的函数(SBC case)
// /任意字符串
// /全角字符串
// /全角空格为12288,半角空格为32
// /其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
public static String ToSBC(String input) {
// 半角转全角:
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 32) {
c[i] = (char) 12288;
continue;
}
if (c[i] < 127)
c[i] = (char) (c[i] + 65248);
}
return new String(c);
}
/**/// /
// / 转半角的函数(DBC case)
// /任意字符串
// /半角字符串
// /全角空格为12288,半角空格为32
// /其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
// /
public static String ToDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
/**
* 主函数做测试用
* @param args String[]
* @throws Exception
*/
public static void main(String[] args) throws Exception {
/* System.out.println(BaseFunction.dateToString(stringToDate(
"2005-5-2 0:0:10")));
System.out.println(BaseFunction.getTime(true));
System.out.println(BaseFunction.continuousMakeString(" ", 10));
System.out.println(BaseFunction.replaceStartsSpace(
" fadsfas dfsd", " "));
*/
/* HashMap hm=BaseFunction.random(1,100,5);
Set set=hm.keySet();
Iterator it=set.iterator();
while(it.hasNext()){
System.out.println(hm.get(it.next()));
}
*/
//System.out.println(BaseFunction.replaceStartsWithSpace(" 中国 湖南 长沙"," "));
// System.out.println(BaseFunction.round(0.25689, 2));
// //java的float、double相加不精确的小数位,使用如下方式解决
// BigDecimal bd=new BigDecimal(12.36645593);
// System.out.println(bd.add(new BigDecimal(1002)).add(new BigDecimal(123.366544)).doubleva lue());
//System.out.println(BaseFunction.chinaToUnicode("注册码"));
//System.out.println(BaseFunction.unicodeToChinese("/u6ce8/u518c/u7801"));
String BJstr = "Hello! 123456 湖南长沙";
String QJstr = ToSBC(BJstr);
System.out.println(BJstr + " 半角转全角 " + QJstr);
System.out.println(QJstr + " 全角转半角 " + ToDBC(QJstr));
}
}
作者:xi_hong_shi