java截取字符串有汉字的,java截取带有汉字的字符串

发布时间:2022-11-21

本文目录一览:

  1. java截取中文字符串。
  2. 求截取带中文的字符串的java方法。
  3. java怎么把字符串中的的汉字取出来
  4. java语言中如何获取字符串中汉字的个数

java截取中文字符串。

public static void main(String args[]) {
    String str = "看看以下回答是否解决了您的疑问";
    // 提取子字符串,头一个表示开头的索引(包括),后一个表示结束的索引(不包括)
    // 索引是在字符串的位置,从0开始
    // str.substring(0, 7)表示从第0位到第6位(因为不包含第七位)
    // 英文字符串也是类似的
    String substr = str.substring(0, 7);
    System.out.println(substr);
}

求截取带中文的字符串的java方法。

楼主你好,很高兴能回答你这个很有挑战性的问题,首先我谈谈我对上面9个例子的理解,即这个方法应该实现的功能: 这个方法与JDK String原有的方法substring是有区别的,在这个方法里,一个中文汉字相当于占2个英文字符的位置。而且根据方法传入的参数pStart和pEnd在返回相应的子字符串child。 如果pstart刚好在某个汉字的前半部分,则child应包含该汉字,在后部分则不含。与之相对应的是pEnd如果在某个汉字的后半部分,则child应含该汉字,否则不包含,如果pStart超出pStr的长度(这里一个汉字长度算2),则返回空,其他性质和JDK的性质相同。 如果觉得我的理解不错,且看下面的代码:

public class Test {
    public Test() {
        String str = "ABCDE";
        String str2 = "ABC你D";
        String str3 = "A你B好C吗勇DE";
        System.out.println("1 str='ABCDE' start=1 end=5 结果:" + getSubString(str, 1, 5));
        System.out.println("2 str='ABCDE' start=1 end=4 结果:" + getSubString(str, 1, 4));
        System.out.println("3 str='ABCDE' start=2 end=4 结果:" + getSubString(str, 2, 4));
        System.out.println("4 str='ABCDE' start=6 end=7 结果:" + getSubString(str, 6, 7));
        System.out.println("5 str='ABCDE' start=5 end=5 结果:" + getSubString(str, 5, 5));
        System.out.println("6 str2='ABC你D' start=1 end=3 结果:" + getSubString(str2, 1, 3));
        System.out.println("7 str2='ABC你D' start=1 end=4 结果:" + getSubString(str2, 1, 4));
        System.out.println("8 str2='ABC你D' start=1 end=5 结果:" + getSubString(str2, 1, 5));
        System.out.println("9 str2='ABC你D' start=4 end=4 结果:" + getSubString(str2, 4, 4));
        System.out.println("10 str3='A你B好C吗勇DE' start=9 end=10 结果:" + getSubString(str3, 9, 10));
    }
    public static void main(String args[]) {
        new Test();
    }
    public String getSubString(String str, int pstart, int pend) {
        String resu = "";
        int beg = 0;
        int end = 0;
        int count1 = 0;
        char[] temp = new char[str.length()];
        str.getChars(0, str.length(), temp, 0);
        boolean[] bol = new boolean[str.length()];
        for (int i = 0; i < temp.length; i++) {
            bol[i] = false;
            if ((int) temp[i] > 255) {
                count1++;
                bol[i] = true;
            }
        }
        if (pstart > str.length() + count1) {
            resu = null;
        }
        if (pstart > pend) {
            resu = null;
        }
        if (pstart < 1) {
            beg = 0;
        } else {
            beg = pstart - 1;
        }
        if (pend > str.length() + count1) {
            end = str.length() + count1;
        } else {
            end = pend;
        }
        if (resu != null) {
            if (beg == end) {
                int count = 0;
                if (beg == 0) {
                    if (bol[0] == true)
                        resu = null;
                    else
                        resu = new String(temp, 0, 1);
                } else {
                    int len = beg;
                    for (int y = 0; y < len; y++) {
                        if (bol[y] == true)
                            count++;
                        len--;
                    }
                    if (count == 0) {
                        if ((int) temp[beg] > 255)
                            resu = null;
                        else
                            resu = new String(temp, beg, 1);
                    } else {
                        if ((int) temp[len + 1] > 255)
                            resu = null;
                        else
                            resu = new String(temp, len + 1, 1);
                    }
                }
            } else {
                int temSt = beg;
                int temEd = end - 1;
                for (int i = 0; i < temSt; i++) {
                    if (bol[i] == true)
                        temSt--;
                }
                for (int j = 0; j < temEd; j++) {
                    if (bol[j] == true)
                        temEd--;
                }
                if (bol[temSt] == true) {
                    int cont = 0;
                    for (int i = 0; i <= temSt; i++) {
                        cont++;
                        if (bol[i] == true)
                            cont++;
                    }
                    if (pstart == cont)
                        temSt++;
                }
                if (bol[temEd] == true) {
                    int cont = 0;
                    for (int i = 0; i <= temEd; i++) {
                        cont++;
                        if (bol[i] == true)
                            cont++;
                    }
                    if (pend < cont)
                        temEd--;
                }
                if (temSt == temEd) {
                    resu = new String(temp, temSt, 1);
                } else if (temSt > temEd) {
                    resu = null;
                } else {
                    resu = str.substring(temSt, temEd + 1);
                }
            }
        }
        return resu;
    }
}

测试结果如图,并且可以任意修改字符串,保证结果正确。 That's all !

java怎么把字符串中的的汉字取出来

  1. 判断字符串是否全是汉字。
String str1 = "java判断是否为汉字";
String str2 = "全为汉字";
String reg = "[\\u4e00-\\u9fa5]+";
boolean result1 = str1.matches(reg); // false
boolean result2 = str2.matches(reg); // true
  1. 提取字符串中的汉字。
String str = "java怎么把asdasd字符串中的asdasd的汉字取出来";
String reg = "[^\\u4e00-\\u9fa5]";
str = str.replaceAll(reg, " ");
System.out.println(str);
  1. 判断字符串中是否含有汉字。
boolean result = (str.length() == str.getBytes().length); // true:无汉字 false:有汉字
  1. 获取字符串中汉字的个数。
int count = 0;
String reg = "[\\u4e00-\\u9fa5]";
String str = "java获取汉字Chinese的个数";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while (m.find()) {
    for (int i = 0; i <= m.groupCount(); i++) {
        count = count + 1;
    }
}
System.out.println("共有汉字 " + count + "个 ");

java语言中如何获取字符串中汉字的个数

【实例描述】 字符串中可以包含数字,字母、汉字或者其他字符。使用Character类的isDigit()方法可以判断字符中的某个字符是否为数字,使用Character类的isLetter()方法可以判断字符中的某个字符是否为字母。实例中将介绍一种方法用来判断字符串中的某个字符是否为汉字,通过此方法可以计算字符串中汉字的数量。实例的运行效果如图4.24所示。 【实现过程】 在Eclipse中新建项目ChineseCharacter,并在其中创建一个ChineseCharacter.java文件。在该类的主方法中创建标准输入流的扫描器对象,接收用户输入的字符串。我们在程序中使用matches()方法来统计该字符串中汉字的个数。核心代码如下所示:

protected void do_button_actionPerformed(ActionEvent e) {
    String text = chineseArea.getText(); // 获取用户输入
    int amount = 0; // 创建汉字数量计数器
    for (int i = 0; i < text.length(); i++) {
        // 使用正则表达式判断字符是否属于汉字编码
        boolean matches = Pattern.matches("^[\u4E00-\u9FA5]{0,}$", "" + text.charAt(i));
        if (matches) { // 如果是汉字
            amount++; // 累加计数器
        }
    }
    umField.setText(amount + ""); // 在文本框显示汉字数量
}

【代码解析】 本实例的关键点在于正则表达式的使用。Java提供了Pattern用于正则表达式的编译表示形式,该类提供的静态方法matches()可以执行正则表达式的匹配。该方法编译给定正则表达式并尝试给定输入与其匹配。如果要匹配的字符序列与正则表达式匹配则返回true,否则返回false。其声明语法如下:

public static boolean matches(String regex, CharSequence input);

【知识扩展】 使用正则表达式可以方便地进行字符串操作,正则表达式经常被用来验证用户输入的信息,如可以判断用户输入的格式是否正确。本实例中使用正则表达式来判断用户输入的字符串是否为汉字,如果为汉字则计数器加1,最后得到字符串中所有汉字的数量。