replaceAll public String replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)Parameters: regex - the regular expression to which this string is to be matched Returns: The resulting String Throws: PatternSyntaxException - if the regular expression's syntax is invalid Since: 1.4 See Also: Pattern
public static String toFullWidth(String halfWidth) { StringBuffer buff = new StringBuffer(halfWidth.length()); for (int i = 0; i < halfWidth.length(); i++) { char c = halfWidth.charAt(i) - '0' + '0'; buff.append(c); } return buff.toString(); }
sorry char c = (char) half.....没有用编译器写,直接在ie里面写的,应该不会有问题
public String wToF (String aW) { String [] tmp = new String [] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; for (int i = 0; i <= 9; i ++) aW = aW.replaceAll (tmp [i], i + ""); return (aW); }String aa = "134754481"; String bb = "134754481";if( bb.equals(wToF (aa))) { System.out.print("相等"); }
1和1对应的anscii码差30(或31记不清楚了)
半角转全角 半角1个字节:byte a = '1'; 全角2个字节:dst[0]=0xA3; dst[1]=(byte)(0x80+a) System.out.println(new String(dst));
先用下面的办法把字符转换成全角在比较 String str = "123"; String ret = ""; for (int i = 0; i < str.length(); i++) { int j = str.charAt(i); if(j<256 && j>-1){ int temp = j + 65248; if (temp > 0) ret += (char) temp; else ret += (char) j; }else ret += (char) j; } System.out.println(str + "|" +ret);
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)Parameters:
regex - the regular expression to which this string is to be matched
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
Pattern
StringBuffer buff = new StringBuffer(halfWidth.length());
for (int i = 0; i < halfWidth.length(); i++) {
char c = halfWidth.charAt(i) - '0' + '0';
buff.append(c);
}
return buff.toString();
}
{
String [] tmp = new String [] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
for (int i = 0; i <= 9; i ++)
aW = aW.replaceAll (tmp [i], i + ""); return (aW);
}String aa = "134754481";
String bb = "134754481";if( bb.equals(wToF (aa)))
{
System.out.print("相等");
}
半角1个字节:byte a = '1';
全角2个字节:dst[0]=0xA3;
dst[1]=(byte)(0x80+a) System.out.println(new String(dst));
String str = "123";
String ret = "";
for (int i = 0; i < str.length(); i++) {
int j = str.charAt(i);
if(j<256 && j>-1){ int temp = j + 65248;
if (temp > 0)
ret += (char) temp;
else
ret += (char) j;
}else
ret += (char) j;
}
System.out.println(str + "|" +ret);
查表?查什么表?Unicode表?
根本就没有什么数字1,字符1。看看汇编吧。
好像也用不上正则表达式吧?谁能给出一个精确的regex?
private static String DBC2SBC(String str)
{ String ret="";
int k=0;
for (int i = 0; i < str.length(); i++) {
int j = str.charAt(i);
if(i>0){
k=str.charAt(i-1);
}
if(j==163){
continue;
}else
{
if((j>=128)&&(k==163)){
ret += (char) (j-128);
}
else
{ret += (char) (j);
}
}
}
return ret;
}
shine333(enihs) 的方法不能处理一个字符串中既有半角又有全角
cuizm(射天狼)的方法,用了10次replaceAll,虽然可以达到目的,还是有点浪费,另外如果不知道aa,bb哪个中有全角,那就要用20次replaceAll:)
//以下代码在ecplise下测试通过
private String BanJiaotoQuanJiao(String strInput){
byte[] aa = strInput.getBytes();
byte[] bb = new byte[aa.length*2];
int temp = 0;
int j = 0;
for (int i = 0 ; i<aa.length ; ++i ) {
if (aa[i] != -93) {
bb[j] = -93;
temp = aa[i] | 0x80;//加上高位1
bb[j+1]= (byte)temp;
j = j+2;
}
}
byte[] result = new byte[j];
System.arraycopy(bb,0,result,0,j);
String strResult = new String(result);
aa = null;
bb = null;
result = null;
return strResult;
}
public static void main(String [] args){
//全角半角混杂
String aa = "1234567891";
String bb = "134754481";
String strResult = BanJiaotoQuanJiao(aa);
System.out.println(strResult);
strResult = BanJiaotoQuanJiao(bb);
System.out.println(strResult);
}
* @author chuayuan88
* 说明:该类实现把字符中的全角转化为半角,包括全角字符,全角空格.
* 在windows中,中文和全角字符都占两个字节,并且使用了ascii chart 2 (codes 128?C255)。
* 我们可以凭这一点来一个个检测用户输入的是否是中文和全角字符。
* 实际上,全角字符的第一个字节总是被置为163,而第二个字节则是相同半角字符码加上128(包 * 括空格)。
* 如半角a为65,则全角a则是163(第一个字节)、193(第二个字节,128+65)。 而对于中文来讲,*它的第一个字节被置为大于163,
* (如'阿'为:176 162),我们可以在检测到中文时不进行转换。
*/
public class EmptyLence {private static int b2i(byte n) {
int m = n;
if (n < 0) {
m = n + 256;
return m;
}
return m;
}private static byte i2b(int n) {
byte m = (byte) n;
if (n > 127) {
m = (byte) (n - 256);
return m;
}
return m;
}private static byte[] getOutputList(byte[] m) { //过滤byte数组中为0的元素
int len = m.length;
StringBuffer temp = new StringBuffer(1024);
for (int i = 0; i < len - 1; i++) {
if (m[i] != 0) {
temp.append(m[i]);
temp.append(",");
}
}
temp.append(m[len - 1]);
StringTokenizer st = new StringTokenizer(temp.toString(), ",");
int total = st.countTokens();
byte[] newList = new byte[total];
for (int i = 0; i < total; i++) {
newList[i] = (byte) Integer.parseInt(st.nextToken());
}
return newList;}public static void main(String[] args) {
String oldStr = "abc def 中国队";
int n = oldStr.getBytes().length;
byte[] strList = new byte[n + 1];
for (int j = 0; j < n; j++) {
strList[j] = oldStr.getBytes()[j];
}
byte[] newList = new byte[n];
int c1, c2;
for (int i = 0; i < newList.length; i++) {
c1 = b2i(strList[i]);
c2 = b2i(strList[i + 1]);if (c1 == 163) { //判断是否为全角字符
newList[i] = i2b(c2 - 128);
i++;
continue;
}if (c1 > 163) { //判断是否为汉字
newList[i] = i2b(c1);
newList[i + 1] = i2b(c2);
i++;
continue;
}if ((c1 == 161) && (c2 == 161))//全角空格是个特例,另加处理
{
newList[i] = 32;
i++;
continue;
}
newList[i] = i2b(c1);}byte[] outputList = getOutputList(newList);
System.out.println(new String(outputList));}}