- 1. 路路通時刻表java版 V8.7 Build 0701 支持多種查詢...
- 2. Javascript學習教程手冊 最好的Javascript學習參考...
- 3. 手機QQ閱讀2011JAVA版(騰訊推出的手機閱讀軟件) VB...
- 4. Java Portable 6 Update 26
- 5. TaskUnifier(java編寫的gtd任務管理工具) V0.9.7 多...
- 6. Java SE Runtime Environment 6 Update 26 x64官方...
- 7. Java SE Runtime Environment 6 Update 26官方免費...
- 8. 我心飛揚類加密工具(java class文件加密工具) V2.0...
- 9. 墨客書蟲(手機閱讀器軟件)for Java V1.10 免費通...
- 10. 多米音樂(手機音樂軟件)for JAVA V1.5.0 中文免費...
Java中全半角之間如何轉(zhuǎn)換
下面提供給大家的是JAVA中全半角之間轉(zhuǎn)換的代碼,相信對剛學JAVA的朋友會有很大幫助。
public class change {
public static void main(String[] args) {
String QJstr="全角轉(zhuǎn)半角DAO";
String result=SBCchange(QJstr);
System.out.println(QJstr+"\n"+result);
}
public static final String SBCchange(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 class Test {
public static void main(String[] args) {
String QJstr = "hello";
String QJstr1 = "hehe";
String result = BQchange(QJstr);
String result1 = QBchange(QJstr1);
System.out.println(QJstr + "\n" + result);
System.out.println(QJstr1 + "\n" + result1);
}
// 半角轉(zhuǎn)全角
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)半角
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;
}
}
/**/// /
// / 轉(zhuǎn)全角的函數(shù)(SBC case)
// /
// /任意字符串
// /全角字符串
// /
// /全角空格為12288,半角空格為32
// /其他字符半角(33-126)與全角(65281-65374)的對應關系是:均相差65248
// /
public static String ToSBC(String input) {
// 半角轉(zhuǎn)全角:
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);
}
/**/// /
// / 轉(zhuǎn)半角的函數(shù)(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);