「java判断gbk」JAVA判断一个数是否在数组中

博主:adminadmin 2023-01-13 16:51:10 328

今天给各位分享java判断gbk的知识,其中也会对JAVA判断一个数是否在数组中进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么判断字符串的字符串的长度

1首先打开eclipse

2新建一个java项目,名字随意起

3名字起好后,点击完成

4右键点击项目名称,新建,类

5

类的名字叫TextLength

包的名字叫 com.zf.s2

点击完成

6

首先要判断是否是汉字

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;is.length();i++){//for循环

c=s.charAt(i);              //获得字符串中的每个字符

if(isChineseChar(c)){//调用方法进行判断是否是汉字

chineseCount++;                 //等同于chineseCount=chineseCount+1

}

}

return chineseCount;                   //返回汉字个数

}

获得字母、数字、空格的个数

7public 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").length1;//汉字的字节数大于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;is.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));                       //调用方法显示其它字母类型的长度

}

}

8

Java: 如何知道一个字符串当前是什么字符集?

判断java字符串的字符集有多种方法,我们一一讨论如下:

1、通过把未知编码字符串,用猜想的编码再解码,观察字符串是不是正确还原了。

原理:假如目标编码没有数组中的字符,那么编码会破坏,无法还原。

缺点:假如字符少,而正巧错误的猜想编码中有这种字节,就会出错。

如:new String("tested str".getBytes("enc"),"enc")

2、大多数时候,我们只要判断本地平台编码和utf8,utf8编码相当有规律,所以可以分析是否是utf8,否则使用本地编码。

原理:分析byte[]来判断规律。

缺点:有时,个别本地编码字节在utf8中也会出现,导致出错,需要分析。

如:判断是否utf-8代码:

public static boolean isValidUtf8(byte[] b,int aMaxCount){

int lLen=b.length,lCharCount=0;

for(int i=0;i

byte lByte=b[i++];//to fast operation, ++ now, ready for the following for(;;)

if(lByte=0) continue;//=0 is normal ascii

if(lByte(byte)0xc0 || lByte(byte)0xfd) return false;

int lCount=lByte(byte)0xfc?5:lByte(byte)0xf8?4

:lByte(byte)0xf0?3:lByte(byte)0xe0?2:1;

if(i+lCountlLen) return false;

for(int j=0;j=(byte)0xc0) return false;

}

return true;

}

3.按编码规则,一字字比照。

优点是错物更少,缺点是太费资源。

java 怎么判断编码是utf-8 还是gbk

你是要判断字符还是判断文件的编码,若是字符:

String

str="123456";

String

type

=

"utf-8";

//更换这里进行其他编码判断

try

{

if

(str.equals(new

String(str.getBytes(type

),

type

)))

{

return

type;

}

}

catch

(Exception

e)

{

}

如果是文件,麻烦一些,可以使用一个开源项目cpdetector,这个我也没用过,你自己查一下吧

技术问题:java中如何判断字符串内容是否是一种编码格式

可以看下java.nio.charset.Charset这个类,这个类里面有个静态方法:

public static boolean isSupported(String charsetName)通知是否支持指定的 charset。

参数:

charsetName - 请求的 charset 名称;可能是规范名称或别名

返回:

当且仅当当前 Java 虚拟机支持指定的 charset 时才返回 true

抛出:

IllegalCharsetNameException - 如果给定的 charset 名称是非法的

IllegalArgumentException - 如果给定的 charsetName 为 null

拿这个静态方法判断并捕捉异常,如果是true那就是正确的,否则就是false

关于java判断gbk和JAVA判断一个数是否在数组中的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。