1. java中String接受的最大字元串的長度是多少
數組最大的長度為Integer.MAX_VALUE
2. Java中文字元所佔的位元組數是多少
採用GB2312或GBK編碼方式時,一個中文字元佔2個位元組;而採用UTF-8編碼方式時,一個中文字元會佔3個位元組。
3. Java中String接受的最大字元串的長度是多少
我們可以使用串接操作符得到一個長度更長的字元串,那麼,string對象最多能容納多少字元呢?查看string的源代碼我們可以得知類string中
是使用域
count
來記錄對象字元的數量,而count
的類型為
int,因此,我們可以推測最長的長度為
2^32,也就是4g。
不過,我們在編寫源代碼的時候,如果使用
sting
str
=
"aaaa";的形式定義一個字元串,那麼雙引號裡面的ascii字元最多隻能
有
65534
個。為什麼呢?因為在class文件的規范中,
constant_utf8_info表中使用一個16位的無符號整數來記錄字元串的長
度的,最多能表示
65536個位元組,而java
class
文件是使用一種變體utf-8格式來存放字元的,null值使用兩個位元組來表示,因此只剩
下
65536-
2
=
65534個位元組。也正是變體utf-8的原因,如果字元串中含有中文等非ascii字元,那麼雙引號中字元的數量會更少(一
個中文字元佔用三個位元組)。如果超出這個數量,在編譯的時候編譯器會報錯
4. 計算java字元串的位元組的長度
publicstaticvoidmain(String[]args)
{
Stringss="hel無此數lo";
byte[]buff=ss.getBytes();
intf=buff.length;
System.out.println(f);
}
可參閱http://blog.csdn.net/xiaoyu714543065/article/details/7380191
5. java中如何計算一個字元串的byte長度(一個漢字多少字元)
中文並不一定是占兩個位元組的,具體佔多少位元組是跟具體的編碼方式相關的。
比如說:GB2312、GBK、GB18030
編碼是佔用兩個位元組的,但是
UTF-8
編碼的話至少需要佔用三個位元組。
有一個簡單方法,就是把大於
0xff
的字元都作為兩個字元(當然是在
GBK
環境下)
Java
code
publicstaticint
count(String
str)
{
if(str
==null||
str.length
==0)
{
return0;
}
int
count
=0;
char[]
chs
=
str.toCharArray();
for(int
i
=0;
i
<
chs.length;
i++)
{
count
+=
(chs[i]
>0xff)
?2
:
1;
}
return
count;
}
或者你直接使用
int
len
=
str.getBytes("gbk");上面代碼假設你的資料庫編碼格式是
GBK
的,而不是
UTF-8
的,如果是
UTF-8
的話,上面的代碼無效!1位元組等於8比特,一個字母是1位元組,而一個漢字要用2個位元組。一個漢字=2個字元
6. Java編程,求字元串的長度
int chineseCount=0;
if(!"".equals("")){//判斷是否為空
s=new String(s.getBytes(),"GBK"); //進行統一編碼
}
for(int i=0;i<s.length();i++){//for循環
c=s.charAt(i); //獲得字元串中的每個字元
if(isChineseChar(c)){//調用方法進行判斷是否是漢字
chineseCount++; //等同於chineseCount=chineseCount+1
}
}
return chineseCount; //返回漢字個數
}
獲得字母、數字、空格的個數
public static String getStringInfo(String s){
char ch;
int character=0,blank=0,number=0;
for(int i=0;i <s.length();i++) //for循環
{
ch=s.charAt(i);
if((ch>='a'&&ch <='z')||(ch>='A'&&ch <='Z'))//統計字母
character++; //等同於character=character+1
else if(ch==' ') //統計空格
blank++; //等同於blank=blank+1
else if(ch>='0'&& ch <='9') //統計數字
number++; //等同於number=number+1;
}
完整代碼
package com.zf.s2;//創建一個包
public class TextLength {//描述字元串長度的類
public static boolean isChineseChar(char c) throws Exception{//判斷是否是一個漢字
return String.valueOf(c).getBytes("GBK").length>1;//漢字的位元組數大於1
}
public static int getChineseCount(String s) throws Exception{//獲得漢字的長度
char c;
int chineseCount=0;
if(!"".equals("")){//判斷是否為空
s=new String(s.getBytes(),"GBK"); //進行統一編碼
}
for(int i=0;i<s.length();i++){//for循環
c=s.charAt(i); //獲得字元串中的每個字元
if(isChineseChar(c)){//調用方法進行判斷是否是漢字
chineseCount++; //等同於chineseCount=chineseCount+1
}
}
return chineseCount; //返回漢字個數
}
public static String getStringInfo(String s){//獲得字母、數字、空格的個數
char ch;
int character=0,blank=0,number=0;
for(int i=0;i <s.length();i++) //for循環
{
ch=s.charAt(i);
if((ch>='a'&&ch <='z')||(ch>='A'&&ch <='Z'))//統計字母
character++; //等同於character=character+1
else if(ch==' ') //統計空格
blank++; //等同於blank=blank+1
else if(ch>='0'&& ch <='9') //統計數字
number++; //等同於number=number+1;
}
return "字元串中共有"+character+"個字母,"+blank+"個空格,"+number+"個數字";
}
public static void main(String []args) throws Exception {//java程序的主入口方法
String s="hello world 世界你好!!123*";
System.out.println("字元串的總長度:"+s.length());//顯示字元串長度
System.out.println("字元串中漢字長度:"+getChineseCount(s)); //調用方法顯示漢字長度
System.out.println(getStringInfo(s)); //調用方法顯示其它字母類型的長度
}
}