# Java 测试不同字符集的长度

**URL:** <https://www.isharkfly.com/t/java/6477>\
**Category:** 程序语言\
**Created:** [March 5, 2011, 11:27am UTC](https://www.isharkfly.com/t/java/6477 "2011-03-05T11:27:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [March 5, 2011, 11:27am UTC](https://www.isharkfly.com/t/java/6477/1 "2011-03-05T11:27:41Z")

</div>

你可以用下面的方法测试下不同类型的编码的字符显示长度。[code]package net.hawebs.dbSync.test;

import java.io.UnsupportedEncodingException;

/\*\*

- 测试类
- 
- @author YUCHENGHU
- 

\*/  
public class HTest {

```
public static void main(String[] args) {
	String aString = "罕布什尔（中国）信息技术有限公司 ，This is a test string.";
	String anotherString = null;
	try {
		anotherString = new String(aString.getBytes("UTF-8"), "ISO8859_1");
	} catch (UnsupportedEncodingException ex) {
	}
	System.out.println(aString.length() + "," + anotherString.length());
}

```

}[/code]运行结果如下：  
 ![](https://www.isharkfly.com/images/transparent.png)
