㈠ java中如何統計一個字元串的長度
使用字元串變數。
㈡ java中如何統計一個字元串的長度
首先打開eclipse
㈢ 簡單的JAVA字元串長度計算的實現
簡單實現代碼如下:
public
class
stringlength
{
/**
*
獲取字元串的長度,如果有中文,則每個中文字元計為2位
*
@param
value
指定的字元串
*
@return
字元串的長度
*/
public
static
int
length(string
value)
{
int
valuelength
=
0;
string
chinese
=
"[\u0391-\uffe5]";
/*
獲取欄位值的長度,如果含中文字元,則每個中文字元長度為2,否則為1
*/
for
(int
i
=
0;
i
<
value.length();
i++)
{
/*
獲取一個字元
*/
string
temp
=
value.substring(i,
i
+
1);
/*
判斷是否為中文字元
*/
if
(temp.matches(chinese))
{
/*
中文字元長度為2
*/
valuelength
+=
2;
}
else
{
/*
其他字元長度為1
*/
valuelength
+=
1;
}
}
return
valuelength;
}
public
static
void
main(string
args[]){
string
str
=
"hello你好";
system.out.println(stringlength.length(str));
}
}
㈣ 如何計算中文字元的長度java
通過String自帶的length()方法獲取字元串長度。
String a="abcdefg";//定義一個字元串
int len = a.length();//通過length獲取字元串長度,這里等於7
length()該方法返回此字元串的長度。長度是等於Unicode代碼單元中的字元串的數目。
㈤ java字元串長度怎麼算
通過string自帶的length()方法獲取字元串長度。
string a="abcdefg";//定義一個字元串
int len = a.length();//通過length獲取字元串長度,這里等於7
length()該方法返回此字元串的長度。長度是等於unicode代碼單元中的字元串的數目。
㈥ 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)); //調用方法顯示其它字母類型的長度
}
}
㈦ java中怎麼計算輸入字元串的長度
通過String自帶的length()方法獲取字元串長度。
String a="abcdefg";//定義一個字元串
int len = a.length();//通過length獲取字元串長度,這里等於7
length()該方法返回此字元串的長度。長度是等於Unicode代碼單元中的字元串的數目。
㈧ 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個字元
㈨ java字元串長度怎麼算
通過String自帶的length()方法獲取字元串長度。
Stringa="abcdefg";//定義一個字元串
intlen=a.length();//通過length獲取字元串長度,這里等於7
㈩ java中怎麼獲取一個字元串的長度
通過String自帶的length()方法獲取字元串長度。
String a="abcdefg";//定義一個字元串
int len = a.length();//通過length獲取字元串長度,這里等於7
length()該方法返回此字元串的長度。長度是等於Unicode代碼單元中的字元串的數目。