社区
Java SE
帖子详情
如何判断一个中文字符串的长度
zhyl1979
2004-11-05 12:11:42
下面这段代码输出的结果是:3
可是我希望的是他的unicode的长度。如何做?
谢谢!
public class temp
{
String str="你好啊";
public temp()
{
System.out.println(str.length());
}
public static void main(String args[])
{
temp obj=new temp();
}
}
...全文
394
6
打赏
收藏
如何判断一个中文字符串的长度
下面这段代码输出的结果是:3 可是我希望的是他的unicode的长度。如何做? 谢谢! public class temp { String str="你好啊"; public temp() { System.out.println(str.length()); } public static void main(String args[]) { temp obj=new temp(); } }
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
PoemCode
2004-11-05
打赏
举报
回复
来看一下文档:
length
public int length()
Returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.
Specified by:
length in interface CharSequence
Returns:
the length of the sequence of characters represented by this object.
注意这么一句话:
The length is equal to the number of 16-bit Unicode characters in the string.
一个汉字只占一个 16-bit Unicode
haroyy
2004-11-05
打赏
举报
回复
Unicode中文长度是不会变的,英文字母从1字节变为2字节
treeroot
2004-11-05
打赏
举报
回复
它的Unicode长度就是3呀每个Unicode占用2个字节,字节数是6
realfish
2004-11-05
打赏
举报
回复
package wuziqi;
import java.io.UnsupportedEncodingException;
public class temp
{
String str="你好啊";
public temp()
{
try {
byte[] a = str.getBytes("UTF-8");
System.out.println(a.length);
} catch (UnsupportedEncodingException e) {
}
}
public static void main(String args[])
{
temp obj=new temp();
}
}
realfish
2004-11-05
打赏
举报
回复
楼主应该是要UTF-8的长度,是1-6变长编码,我上面的编码可行。
我觉得楼主在做插入数据库之前的长度判断。
土不锅锅
2004-11-05
打赏
举报
回复
看看这个例子:
import java.io.*;
public class StringTest
{
public static void main(String[] args)
{
String aString = "这是一个测试串,This is a test string.";
String anotherString = null;
try {
anotherString = new String(aString.getBytes("GBK"), "ISO8859_1");
}
catch (UnsupportedEncodingException ex) {}
System.out.println(aString.length() + "," + anotherString.length());
}
}
Python 47个
字符串
方法
# Python 47个
字符串
方法## 课程介绍同学们是否总是要搜资料、查官网后,才知道怎么操作
字符串
?如果是,来学学本课程吧,本课程以讲解官网文档的方式,讲解 Python 47个
字符串
方法,涉及替换、删除、连接、查找、...
js
判断
字符串
长度
的方法
js中
判断
字符串
的
长度
一般都是使用 length 属性 ,但是 length 属性获取到的
字符串
长度
是以字节为单位,如果我们想要利用 js 根据字节来获取或
判断
一串
字符串
的
长度
该如何做呢? js 按字符
判断
一串
字符串
的
长度
...
php
字符串
长度
判断
_php
字符串
长度
判断
更高效的方法
在php里当需要
判断
一个
字符串
长度
时,我们首先想到的是strlen()函数,不错,strlen()返回的就是
字符串
的
长度
,这样使用没有任何问题。不过,如果要从php程序优化的角度来看,使用strlen()
判断
字符串
长度
未免是最好的...
java
判断
中文
字符串
长度
_java
判断
中文
字符串
长度
的简单实例
话不多说,上代码:/*** 获取
字符串
的
长度
,如果有
中文
,则每个
中文
字符计为2位* @param value 指定的
字符串
* @return
字符串
的
长度
*/public static int length(String value) {int valueLength = 0;String chinese ...
较真:js
判断
中文
字符串
长度
的正确方法
对于使用javascript编程的人来说,
判断
字符串
长度
应该是常用又简单的操作,但在
判断
包含有
中文
字符或其他非asci字符的
字符串
长度
时,却有些坑在里面。
Java SE
62,614
社区成员
307,326
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章