導航:首頁 > 編程語言 > java字元串包含中文

java字元串包含中文

發布時間:2023-08-20 05:45:36

1. java判斷字元串中是否含有中文

publicclasstest{

publicstaticvoidmain(String[]args){
System.out.println(isContainsChinese("122地點"));
}

//方法返回true為包含中文;false不包含
(Stringstr)
{
Patternpat=Pattern.compile("[u4e00-u9fa5]");
Matchermatcher=pat.matcher(str);
booleanflg=false;
if(matcher.find()){
flg=true;
}
returnflg;
}
}

2. Java 判斷字元串中是否含有漢字

Java判斷字元串中是否包含漢字

Java代碼
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IfHanZi {

public static void main(String[] args) {
//方法一:

String s1 = "我是中國人";
String s2 = "imchinese";
String s3 = "im中國人";
System.out.println(s1 + ":" + new String(s1).length());
System.out.println(s2 + ":" + new String(s2).length());
System.out.println(s3 + ":" + new String(s3).length());
System.out.println((s1.getBytes().length == s1.length()) ? "s1無漢字":"s1有漢字");
System.out.println((s2.getBytes().length == s2.length()) ? "s2無漢字":"s2有漢字");
System.out.println((s3.getBytes().length == s3.length()) ? "s3無漢字":"s3有漢字");

//方法二:

int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
String str = "中文fd我是中國人as ";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count = count + 1;
}
}
System.out.println("共有 " + count + "個 ");
}
}

3. java中獲取含中文的字元串長度

publicclassSplitTest{

publicstaticvoidmain(String[]args){

Strings="abc,大家好abc才是真的好abc";

s=s.replaceAll("[^(\u4e00-\u9fa5)]","");
System.out.println(s+" 長度="+s.length());
}
}

4. java判斷字元串中是否有中文

把要判斷的字元串放入List裡面,然後遍歷list集合,如果還有指定的字元就輸出,如下代碼:

packagecom.qiu.lin.he;

importjava.util.ArrayList;
importjava.util.List;

publicclassCeshi{
publicstaticvoidmain(String[]args){
List<String>list=newArrayList<String>();//新建一個集合
list.add("puton");
list.add("inonputin");
list.add("oneputonininputoutoutput");

for(Strings:list){
if(s.indexOf("puton")!=-1){//如果含有連續的字元puton則輸出yes
System.out.println(s+"----yes");
}else{
System.out.println(s+"----no");
}

}
}
}

運行結果如下:

5. java判斷字元串中是否含有漢字

@@@1@@@

intcount=0;
StringregEx="[\u4e00-\u9fa5]";
Stringstr="中文fd我是中國人as";
Patternp=Pattern.compile(regEx);
Matcherm=p.matcher(str);
while(m.find()){
for(inti=0;i<=m.groupCount();i++){
count=count+1;
}
}
System.out.println("共有"+count+"個");
}


@@@2@@@

packagecn.sunzn.demo;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassDemo{
publicstaticvoidmain(String[]args){
System.out.println(isContainChinese("中國China"));
}
(Stringstr){
Patternp=Pattern.compile("[u4e00-u9fa5]");
Matcherm=p.matcher(str);
if(m.find()){
returntrue;
}
returnfalse;
}
}

6. java里怎麼判斷一個字元串里有某個以漢字表達的關鍵字。。

java所有字元都是用Unicode編碼表示,相等直接用equals比較,例如str.equalse("漢字")。包含某個漢字用str.indexOf("漢字");,返回值不是負1表示字元串str包含「漢字」兩個字

閱讀全文

與java字元串包含中文相關的資料

熱點內容
linux切換db2用戶命令 瀏覽:306
相片如何用電解壓 瀏覽:905
碩士程序員去學校當老師 瀏覽:120
pythonstr提取到字典 瀏覽:818
程序員那麼可愛有人看上陸漓了 瀏覽:876
php正則提取圖片 瀏覽:103
pythonlinuxdjango 瀏覽:562
php中文返回亂碼 瀏覽:89
宿舍裝的電信怎麼加密 瀏覽:745
為什麼壓縮文件解壓後變少了 瀏覽:426
現在安卓充電器普遍是什麼型號 瀏覽:714
9日均線36均線主圖指標源碼 瀏覽:349
程序員阿里文化完整版 瀏覽:98
早間新聞在哪個app上面可以看 瀏覽:954
工作啦app注冊的信息怎麼刪去 瀏覽:378
滾動轉子式製冷壓縮機 瀏覽:873
美國編程用什麼軟體 瀏覽:571
圖片加密防盜用 瀏覽:616
dbscan演算法python源碼 瀏覽:849
固態硬碟文件夾刪不掉 瀏覽:717