java中计算字符串长度的方法及u4E00与u9FBB的认识

2016-02-19 11:39 9 1 收藏

今天图老师小编要跟大家分享java中计算字符串长度的方法及u4E00与u9FBB的认识,精心挑选的过程简单易学,喜欢的朋友一起来学习吧!

【 tulaoshi.com - 编程语言 】

字符串采用unicode编码的方式时,计算字符串长度的方法找出UNICODE编码中的汉字的代表的范围“u4E00” 到“u9FBB”之间,由于一个汉字代表两个字符所以可以采用如下方法进行计算字符串的长度
代码如下:

public static int getCharLength(String content) {
int count = 0;
for (int i = 0, len = content.length(); i len; ++i) {
if ('u4E00' = content.charAt(i) && 'u9FBB' = content.charAt(i)) {
count++;
}
}
return content.length()+count;
}

其它的编码方式可以按照这个原理去时行计算

来源:https://www.tulaoshi.com/n/20160219/1598091.html

延伸阅读
标签: Web开发
function cutstr(thestr,strlen) dim l,t,c l=len(thestr) t=0 for dxy=1 to l c=Abs(asc(Mid(thestr,dxy,1))) if c255 then t=t+2 else t=t+1 end if if t=strlen then thev=left(thestr,dxy) exit for else bu=strlen-t for bui=1 to bu strbu=" " strbuall=strbuall&strbu next thev=thestr&strbuall strbu="" strbuall="" end if next...
(1).String 和StringBuffer对象的创建! (2).String类的常用方法:       length(), charAt(), getchars(), equals(),starsWith(), endsWith(), indexOf(), laseIndex(), substring(),contact()       replace(), valueOf(). (3).StringBuffer类的常用方法: &nbs...
标签: Web开发
我们在实际过程中要经常去检测含有中文字符串的实际长度, 因为中文字符,和英文字符在一起的话.判断起来你麻烦点. 原理跟在asp中的一样.逐个去判断.是中文加2英文加1. 代码如下: ASCIIEncoding n = new ASCIIEncoding(); byte[] b = n.GetBytes(str); int l = 0; // l ...
代码如下: package com.gpdi.action; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class WordsStatistics {     class Obj {         int count ;     ...
题目:定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。如把字符串abcdef左旋转2位得到字符串cdefab。请实现字符串左旋转的函数。要求时间对长度为n的字符串操作的复杂度为O(n),辅助内存为O(1)。 分析:如果不考虑时间和空间复杂度的限制,最简单的方法莫过于把这道题看成是把字符串分成前后两部分,通过旋转操作把这...

经验教程

509

收藏

91
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部