java 字节 字符_Java 字节 字符 字符串

String的length()方法和数组的length属性

String类有length()方法吗?数组有length()方法吗?

String类当然有length()方法了,看看String类的源码就知道了,这是这个方法的定义:

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

publicintlength() {

returncount;

}

public int length() {

return count;

}

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

中文汉字在char中的保存

一个中文汉字能保存在一个char类型里吗?

请看下面的例子:

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

publicclassChineseTest {

publicstaticvoidmain(String[] args) {

// 将一个中文汉字赋值给一个char变量

chara ='中';

charb ='文';

charc ='测';

chard ='试';

chare ='成';

charf ='功';

System.out.print(a);

System.out.print(b);

System.out.print(c);

System.out.print(d);

System.out.print(e);

System.out.print(f);

}

}

public class ChineseTest {

public static void main(String[] args) {

// 将一个中文汉字赋值给一个char变量

char a = '中';

char b = '文';

char c = '测';

char d = '试';

char e = '成';

char f = '功';

System.out.print(a);

System.out.print(b);

System.out.print(c);

System.out.print(d);

System.out.print(e);

System.out.print(f);

}

}

编译没有报错,运行结果:

中文测试成功

答案就不用说了。为什么一个中文汉字可以保存在一个char变量里呢?因为在JAVA中,一个char是2个字节(byte),而一个中文汉字是一个字符,也是2个字节。而英文字母都是一个字节的,因此它也能保存到一个byte里,一个中文汉字却不能。请看:

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

publicclassChineseTest {

publicstaticvoidmain(String[] args) {

// 将一个英文字母赋值给一个byte变量

bytea ='a';

// 将一个中文汉字赋值给一个byte变量时,编译会报错

// byte b = '中';

System.out.println("byte a = "+ a);

// System.out.println("byte b = "+b);

}

}

public class ChineseTest {

public static void main(String[] args) {

// 将一个英文字母赋值给一个byte变量

byte a = 'a';

// 将一个中文汉字赋值给一个byte变量时,编译会报错

// byte b = '中';

System.out.println("byte a = " + a);

// System.out.println("byte b = "+b);

}

}

运行结果:

byte a = 97

正如大家所看到的那样,我们实际上是把字符'a'对应的ASCII码值赋值给了byte型变量a。

让我们回过头来看看最初的例子,能不能将a、b、c、d、e、f拼接在一起一次输出呢?让我们试试看:

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

publicclassChineseTest {

publicstaticvoidmain(String[] args) {

// 将一个中文汉字赋值给一个char变量

chara ='中';

charb ='文';

charc ='测';

chard ='试';

chare ='成';

charf ='功';

System.out.print(a + b + c + d + e + f);

}

}

public class ChineseTest {

public static void main(String[] args) {

// 将一个中文汉字赋值给一个char变量

char a = '中';

char b = '文';

char c = '测';

char d = '试';

char e = '成';

char f = '功';

System.out.print(a + b + c + d + e + f);

}

}

运行结果:

156035

这显然不是我们想要的结果。只所以会这样是因为我们误用了“+”运算符,当它被用于字符串和字符串之间,或者字符串和其他类型变量之间时,它产生的效果是字符串的拼接;但当它被用于字符和字符之间时,效果等同于用于数字和数字之间,是一种算术运算。因此我们得到的“156035”是'中'、'文'、'测'、'试'、'成'、'功'这六个汉字分别对应的数值算术相加后的结果。

字符串的反转输出

这也是面试题中常考的一道。我们就以一个包含了全部26个英文字母,同时又具有完整含义的最短句子作为例子来完成解答。先来看一下这个句子:

引用

A quick brown fox jumps over the lazy dog.(一只轻巧的棕色狐狸从那条懒狗身上跳了过去。)

最常用的方式就是反向取出每个位置的字符,然后依次将它们输出到控制台:

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

publicclassStringReverse {

publicstaticvoidmain(String[] args) {

// 原始字符串

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

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

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

for(inti = s.length(); i >0; i--) {

System.out.print(s.charAt(i -1));

}

// 也可以转换成数组后再反转,不过有点多此一举

char[] data = s.toCharArray();

System.out.println();

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

for(inti = data.length; i >0; i--) {

System.out.print(data[i -1]);

}

}

}

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("反转后字符串:");

for (int i = s.length(); i > 0; i--) {

System.out.print(s.charAt(i - 1));

}

// 也可以转换成数组后再反转,不过有点多此一举

char[] data = s.toCharArray();

System.out.println();

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

for (int i = data.length; i > 0; i--) {

System.out.print(data[i - 1]);

}

}

}

运行结果:

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

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

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

以上两种方式虽然常用,但却不是最简单的方式,更简单的是使用现有的方法:

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

publicclassStringReverse {

publicstaticvoidmain(String[] args) {

// 原始字符串

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

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

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

StringBuffer buff =newStringBuffer(s);

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

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

}

}

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

按字节截取含有中文汉字的字符串

要求实现一个按字节截取字符串的方法,比如对于字符串"我ZWR爱JAVA",截取它的前四位字节应该是"我ZW",而不是"我ZWR",同时要保证不会出现截取了半个汉字的情况。

英文字母和中文汉字在不同的编码格式下,所占用的字节数也是不同的,我们可以通过下面的例子来看看在一些常见的编码格式下,一个英文字母和一个中文汉字分别占用多少字节。

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

importjava.io.UnsupportedEncodingException;

publicclassEncodeTest {

/**

* 打印字符串在指定编码下的字节数和编码名称到控制台

*

* @param s

*            字符串

* @param encodingName

*            编码格式

*/

publicstaticvoidprintByteLength(String s, String encodingName) {

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

try{

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

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}

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

}

publicstaticvoidmain(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");

}

}

import java.io.UnsupportedEncodingException;

public class EncodeTest {

/**

* 打印字符串在指定编码下的字节数和编码名称到控制台

*

* @param s

* 字符串

* @param encodingName

* 编码格式

*/

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

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七种编码方案。JAVA所采用的编码方案是UTF-16BE。从上例的运行结果中我们可以看出,GB2312、GBK、GB18030三种编码格式都可以满足题目的要求。下面我们就以GBK编码为例来进行解答。

如果我们直接按照字节截取会出现什么情况呢?我们来测试一下:

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

importjava.io.UnsupportedEncodingException;

publicclassCutString {

publicstaticvoidmain(String[] args)throwsUnsupportedEncodingException {

String s ="我ZWR爱JAVA";

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

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

byte[] tmp =newbyte[6];

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

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

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

s =newString(tmp);

System.out.println(s);

}

}

import java.io.UnsupportedEncodingException;

public class CutString {

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

String s = "我ZWR爱JAVA";

// 获取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?

在截取前六个字节时,第二个汉字“爱”被截取了一半,导致它无法正常显示了,这样显然是有问题的。

我们不能直接使用String类的substring(int beginIndex, int endIndex)方法,因为它是按字符截取的。'我'和'Z'都被作为一个字符来看待,length都是1。实际上我们只要能区分开中文汉字和英文字母,这个问题就迎刃而解了,而它们的区别就是,中文汉字是两个字节,英文字母是一个字节。

Java代码 7d8a0dc35f9ce1ed6fce4b69b49c2354.gif

importjava.io.UnsupportedEncodingException;

publicclassCutString {

/**

* 判断是否是一个中文汉字

*

* @param c

*            字符

* @return true表示是中文汉字,false表示是英文字母

* @throws UnsupportedEncodingException

*             使用了JAVA不支持的编码格式

*/

publicstaticbooleanisChineseChar(charc)

throwsUnsupportedEncodingException {

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

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

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

}

/**

* 按字节截取字符串

*

* @param orignal

*            原始字符串

* @param count

*            截取位数

* @return 截取后的字符串

* @throws UnsupportedEncodingException

*             使用了JAVA不支持的编码格式

*/

publicstaticString substring(String orignal,intcount)

throwsUnsupportedEncodingException {

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

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

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

orignal =newString(orignal.getBytes(),"GBK");

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

if(count >0&& count 

StringBuffer buff =newStringBuffer();

charc;

for(inti =0; i 

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

c = orignal.charAt(i);

buff.append(c);

if(CutString.isChineseChar(c)) {

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

--count;

}

}

returnbuff.toString();

}

}

returnorignal;

}

publicstaticvoidmain(String[] args) {

// 原始字符串

String s ="我ZWR爱JAVA";

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();

}

}

}

import java.io.UnsupportedEncodingException;

public class CutString {

/**

* 判断是否是一个中文汉字

*

* @param c

* 字符

* @return true表示是中文汉字,false表示是英文字母

* @throws UnsupportedEncodingException

* 使用了JAVA不支持的编码格式

*/

public static boolean isChineseChar(char c)

throws UnsupportedEncodingException {

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

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

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

}

/**

* 按字节截取字符串

*

* @param orignal

* 原始字符串

* @param count

* 截取位数

* @return 截取后的字符串

* @throws UnsupportedEncodingException

* 使用了JAVA不支持的编码格式

*/

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爱JAVA";

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爱JAVA

截取前1位:我

截取前2位:我

截取前4位:我ZW

截取前6位:我ZWR爱

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-06-23 10:07

浏览 5697

评论

4 楼

printfabcd

2012-04-25

我还是有点疑惑 一个char最大65535 那是不是说,java 能表示的字符,最多就是65535这么多呢?

3 楼

xiaobadi

2012-03-26

厉害,受教了,谢谢

38687d1a1ad71d37c86f287056834d1a.gif

2 楼

zwj_lmss

2011-03-22

有的代码放了两次,不知道是什么意思

1 楼

zwj_lmss

2011-03-22

这次算见到非常全面的java字符,字节分析了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值