首页 > 编程知识 正文

java判断字符串是否包含中文,java字符串根据逗号截取

时间:2023-05-04 16:35:35 阅读:39713 作者:2832

33558 www.zhi Hu.com/question/20451870。 如果以字节数切割中文字符串,则可能会切割一半或三分之一的汉字而导致乱码。

package com.java.string.split;

import java.util.ArrayList;

import java.util.List;

公共类划分{

publicstaticfinalstringchinesestring='我是testString哈哈';

publicstaticvoidmain (字符串[ ] args ) {

listsplitstringlist=chinesesplitfunction (Chinese string,5 );

for (stringsplit 3360 split string list ) {

system.out.println(split;

}

}

publicstaticlistchinesesplitfunction (字符串src,内字节) )。

try {

if(src==null ) {

返回空值;

}

列表剥离列表=new ArrayList (;

int startIndex=0; //字符串剪切开始位置

int endIndex=bytes src.length (? src.length (:字节; //字符串剪切结束位置

while(startindexsrc.length () ) )。

string substring=src.substring (startindex,endIndex );

//如果要剪切字符串的字节长度长于需要剪切的长度,则包含中文字符

//gbk编码中,中文字符1个占2字节,UTF-8编码格式,中文字符1个占3字节。

while(substring.getbytes(gbk ).length bytes ) )

- -结束索引;

substring=src.substring (startindex,endIndex;

}

splitlist.add(src.substring ) startindex,endIndex );

startIndex=endIndex;

//结束位置与字符串的长度进行比较来判断(src.length ) ) ),与字符串的bytes的长度进行比较,发生了越境异常。

结束索引=(startindex bytes ) src.length )?

src.length (: startindex bytes;

}

返回剥离列表;

}catch(exceptione ) {

e .打印堆栈跟踪(;

}

返回空值;

}

}

以下是另一种处理方法: http://www.blogjava.net/king kong/archive/2009/09/22/296088.html

共享到:

2015-05-17 00:14

阅览4600

评论

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。