您的位置:首页 > 其它

关于string的一些细节

2012-10-26 07:50 211 查看
String有length()方法,数组有length属性

String的长度实际上就是它的属性--char型数组value的长度。数组是没有length()方法的,大家知道,在J***A中,数组也被作为对象来处理,它的方法都继承自Object类。数组有一个属性length,这也是它唯一的属性,对于所有类型的数组都是这样。

在J***A中,一个char是2个字节(byte),而一个中文汉字是一个字符,也是2个字节。所以可以把汉字赋值给char。而英文字母都是一个字节的,因此它也能保存到一个byte里,一个中文汉字却不能。

char型字符单独在输出语句时,输出它的字符本身,与+相连时,输出它的ASCII码值。

UTF-16BE和UTF-16LE是UNICODE编码家族的两个成员。UNICODE标准定义了UTF-8、UTF-16、UTF-32三种编码格式,共有UTF-8、UTF-16、UTF-16BE、UTF-16LE、UTF-32、UTF-32BE、UTF-32LE七种编码方案。J***A所采用的编码方案是UTF-16BE。

substring(int beginIndex, int endIndex)方法,是按字符截取的,不是按字节。一个汉字和字母都看作一个字符。



字符串的反转输出的简单方法:

public class StringReverse {


public static void main(String[] args) {


// 原始字符串

String s = "A quick brown fox jumps over the lazy dog.";


System.out.println("原始的字符串:" + s);




System.out.print("反转后字符串:");


StringBuffer buff = new StringBuffer(s);


// java.lang.StringBuffer类的reverse()方法可以将字符串反转

System.out.println(buff.reverse().toString());

}

}

运行结果:

原始的字符串:A quick brown fox jumps over the lazy dog.

反转后字符串:.god yzal eht revo spmuj xof nworb kciuq A

字符编码例:

import java.io.UnsupportedEncodingException;

public class EncodeTest {




public static void printByteLength(String s, String encodingName) {


System.out.print("字节数:");


try {


System.out.print(s.getBytes(encodingName).length);


} catch (UnsupportedEncodingException e) {


e.printStackTrace();


}

System.out.println(";编码:" + encodingName);


}



public static void main(String[] args) {


String en = "A";


String ch = "人";




// 计算一个英文字母在各种编码下的字节数

System.out.println("英文字母:" + en);


EncodeTest.printByteLength(en, "GB2312");


EncodeTest.printByteLength(en, "GBK");


EncodeTest.printByteLength(en, "GB18030");


EncodeTest.printByteLength(en, "ISO-8859-1");


EncodeTest.printByteLength(en, "UTF-8");


EncodeTest.printByteLength(en, "UTF-16");


EncodeTest.printByteLength(en, "UTF-16BE");


EncodeTest.printByteLength(en, "UTF-16LE");




System.out.println();



// 计算一个中文汉字在各种编码下的字节数

System.out.println("中文汉字:" + ch);


EncodeTest.printByteLength(ch, "GB2312");


EncodeTest.printByteLength(ch, "GBK");


EncodeTest.printByteLength(ch, "GB18030");


EncodeTest.printByteLength(ch, "ISO-8859-1");


EncodeTest.printByteLength(ch, "UTF-8");


EncodeTest.printByteLength(ch, "UTF-16");


EncodeTest.printByteLength(ch, "UTF-16BE");


EncodeTest.printByteLength(ch, "UTF-16LE");


}

}

运行结果如下:

英文字母:A

字节数:1;编码:GB2312

字节数:1;编码:GBK

字节数:1;编码:GB18030

字节数:1;编码:ISO-8859-1

字节数:1;编码:UTF-8

字节数:4;编码:UTF-16

字节数:2;编码:UTF-16BE

字节数:2;编码:UTF-16LE

中文汉字:人

字节数:2;编码:GB2312

字节数:2;编码:GBK

字节数:2;编码:GB18030

字节数:1;编码:ISO-8859-1

字节数:3;编码:UTF-8

字节数:4;编码:UTF-16

字节数:2;编码:UTF-16BE

字节数:2;编码:UTF-16LE

字符截取例:

import java.io.UnsupportedEncodingException;



public class CutString {

public static void main(String[] args) throws UnsupportedEncodingException {

String s = "我ZWR爱J***A";

// 获取GBK编码下的字节数据

byte[] data = s.getBytes("GBK");

byte[] tmp = new byte[6];

// 将data数组的前六个字节拷贝到tmp数组中

System.arraycopy(data, 0, tmp, 0, 6);

// 将截取到的前六个字节以字符串形式输出到控制台

s = new String(tmp);

System.out.println(s);

}

}

输出结果:

我ZWR?

例2:

import java.io.UnsupportedEncodingException;




public class CutString {






public static boolean isChineseChar(char c)


throws UnsupportedEncodingException {


// 如果字节数大于1,是汉字

// 以这种方式区别英文字母和中文汉字并不是十分严谨,但在这个题目中,这样判断已经足够了

return String.valueOf(c).getBytes("GBK").length > 1;


}





public static String substring(String orignal, int count)


throws UnsupportedEncodingException {


// 原始字符不为null,也不是空字符串

if (orignal != null && !"".equals(orignal)) {


// 将原始字符串转换为GBK编码格式

orignal = new String(orignal.getBytes(), "GBK");


// 要截取的字节数大于0,且小于原始字符串的字节数

if (count > 0 && count < orignal.getBytes("GBK").length) {


StringBuffer buff = new StringBuffer();


char c;


for (int i = 0; i < count; i++) {


// charAt(int index)也是按照字符来分解字符串的

c = orignal.charAt(i);


buff.append(c);


if (CutString.isChineseChar(c)) {


// 遇到中文汉字,截取字节总数减1

--count;


}


}


return buff.toString();


}

}

return orignal;


}



public static void main(String[] args) {


// 原始字符串

String s = "我ZWR爱J***A";


System.out.println("原始字符串:" + s);


try {


System.out.println("截取前1位:" + CutString.substring(s, 1));


System.out.println("截取前2位:" + CutString.substring(s, 2));


System.out.println("截取前4位:" + CutString.substring(s, 4));


System.out.println("截取前6位:" + CutString.substring(s, 6));


} catch (UnsupportedEncodingException e) {


e.printStackTrace();


}

}

}

运行结果:

原始字符串:我ZWR爱J***A

截取前1位:我

截取前2位:我

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: