java 字符串实例练习

  • java 字符串操作的一些实例练习:
package com.mgk.string;

import java.util.StringTokenizer;

public class StringMethodTest {
    public static void main(String args[]) {
        String str1 = "hello";
        String str3 = "HELLO";
        String str4 = "";
        String str5 = "  "; // 中间有两个空格
        String str7 = "hello world";

        System.out.println("+++++++++++++isEmpty()+++++++++++++");
        System.out.println( str4.isEmpty() ); // true 判断字符是否为空,为空返回true,反之为false
        System.out.println( str5.isEmpty() ); // false

        System.out.println("+++++++++++++length()+++++++++++++");
        System.out.println( str4.length() );  // 0  获取字符串长度,为0,则为空字符串
        System.out.println( str5.length() );  // 2  获取长度 2个空格

        System.out.println("+++++++++++++contains()+++++++++++++");
        System.out.println( str7.contains(str1) ); // true 判断字符串中是否包含字符或字符序列,区分大小写
        System.out.println( str7.contains("LL") ); // false

        System.out.println("+++++++++++++trim()+++++++++++++");
        String str8 = "  hello  ";
        System.out.println( str8.trim()); // hello 去除字符串左右两边的空白符

        System.out.println("+++++++++++++toLowerCase()+++++++++++++");
        System.out.println( str3.toLowerCase() ); // hello 转成小写

        System.out.println("+++++++++++++toUpperCase()+++++++++++++");
        System.out.println( str1.toUpperCase() ); // HELLO 转成大写

        System.out.println("+++++++++++++valueOf()+++++++++++++");
        double d = 100.00;
        boolean b = true;
        long l = 1234567890;
        char[] arr = {'h','e','l','l','o'};   // 返回参数的字符串表示形式。
        System.out.println(String.valueOf(d)); // 100.0 指定的参数类型(boolean、int、Long、char、char数组、double、float、obj)
        System.out.println(String.valueOf(b)); // true
        System.out.println(String.valueOf(l)); // 1234567890
        System.out.println(String.valueOf(arr)); // hello

        System.out.println("+++++++++++++toString()+++++++++++++");
        String str = new String("www.baidu.com");
        System.out.println( str.toString() ); //www.baidu.com 返回此对象本身

        System.out.println("+++++++++++++toCharArray()+++++++++++++");
        String strr = new String("www.xiaomi.com");
        System.out.println( strr.toCharArray()); //www.xiaomi.com 将字符串转化成字符数组

        System.out.println("+++++++++++++substring()+++++++++++++");
        String str9 = "www.baidu.com";
        System.out.println( str9.substring(4) ); // baidu.com  //str.substring(index1,index2);
        System.out.println( str9.substring(4,9) ); // baidu  //index1从0开始,包含; index2可以省略则到结束,为值则不包含此值。

        System.out.println("+++++++++++++subSequence()+++++++++++++");
        // 返回一个新的字符序列,他是此序列的一个子序列。
        String ostr = "www.baidu.com";
        System.out.println( ostr.subSequence(4,9) ); // baidu (包括起始索引,不包括结束索引);

        System.out.println("*****************startsWith()*****************");
        // 检测字符串是否以指定的前缀开始。
        System.out.println( ostr.startsWith("www") ); // true
        System.out.println( ostr.startsWith("baidu") ); // false
        System.out.println( ostr.startsWith("baidu", 4) ); // true 字符串中开始查找的位置

        System.out.println("*****************endsWith()*****************");
        // 用于测试字符串是否以指定的后缀结束
        System.out.println( ostr.endsWith("com") ); // true
        System.out.println( ostr.endsWith("cn") ); // false

        System.out.println("*********************compareTo***********************");
        // 比较字符串,如果第一个字符和参数的第一个字符不等,比较结束,返回他们之间的长度差值,返回整型,如果第一个字符相等则比较第二个字符,以此类推。
        // 参数字符串等于此字符串,则返回0,
        // 此字符串小于参数字符串,则返回一个小于0的值;
        // 此字符串大于参数字符串,则返回一个大于0的值;
        System.out.println( str1.compareTo(str3)); //32
        System.out.println( str1.compareToIgnoreCase(str3)); // 0  忽略大小写
        System.out.println( str7.compareTo(str3)); // 32

        System.out.println("********************charAt(index)**********************");
        // 返回指定索引的字符,index为0到length-1;
        System.out.println( ostr.charAt(4)); //b

        System.out.println("********************concat(str)**********************");
        // 将参数字符串连接到指定字符串结尾, 一般是 + 来连接
        System.out.println( str1.concat(" java") );

        System.out.println("********************contentEquals()**********************");
        // 用于将此字符串与指定的 StringBuffer 比较。
        String sa = "string1";
        String sb = "string2";
        StringBuffer sc = new StringBuffer("string1");
        System.out.println( sa.contentEquals(sc) );  // true
        System.out.println( sb.contentEquals(sc) );  // false

        System.out.println("********************copyValueOf()**********************");
        // 返回指定数组中表示该字符序列的字符串。
        char[] strArr = {'h','e','l','l','o',' ','j','a','v','a'};
        String sk = "";
        System.out.println( sk.copyValueOf(strArr) );
        System.out.println( sk.copyValueOf(strArr, 2,6)); // 2偏移下标,6偏移总个数

        System.out.println("********************equals()**********************");
        // 用于比较两个字符串的内容是否相等
        String stra1 = new String("java");
        String stra2 = stra1;
        String stra3 = new String("java");
        System.out.println( stra1.equals(stra2) ); // true
        System.out.println( stra1.equals(stra3) ); // true

        System.out.println("**************使用 == 和 equals()比较字符串****************");
        String s1 = "hello";
        String s2 = "hello";
        String s3 = s1;
        String s4 = new String("hello");
        String s5 = new String("hello");

        System.out.println( s1 == s1 ); // true, 相同引用
        System.out.println( s1 == s2 ); // true, 都在公共池,相同引用
        System.out.println( s1 == s3 ); // true,相同引用
        System.out.println( s1 == s4 ); // false,不同应用地址(公共池、堆)
        System.out.println( s4 == s5 ); // false 堆中不同引用
        System.out.println( s1.equals(s3) ); // true ,相同内容
        System.out.println( s1.equals(s4) ); // true , 相同内容
        System.out.println( s4.equals(s5) ); // true ,相同内容

        System.out.println("***************equalsIgnoreCase()*****************");
        // 将一个字符串与另一比较不考虑大小写
        String s6 = new String("HELLO");
        System.out.println( s6.equals(s5) ); // false 区分大小写
        System.out.println( s6.equalsIgnoreCase(s5) ); //true;

        System.out.println("***************getChars()*****************");
        // 将字符从字符串复制到目标字符数组。
        String strc = new String("www.baidu.com");
        char[] strv = new char[6];
        strc.getChars(4,10, strv, 0);
        System.out.println( strv ); // baidu. 4:从strc下标开始位置复制,10:strc下标结束位置,strv 目标数组,0目标数组的起始位置

        System.out.println("***************hasCode()*****************");
        // 返回字符串的哈希码
        System.out.println( strc.hashCode() ); //270263191
        String kstr = "";
        System.out.println( kstr.hashCode() ); // 0 空字符串的哈希值为 0。

        System.out.println("***************indexOf()*****************");
        // indexOf( ch(str) , fromIndex) // 返回指定字符在字符串中第一次出现处的索引,如果没有这样的字符,返回 -1;
        String x = new String("java教程:www.java.com");
        String y = new String("java");
        String z = new String("com");
        System.out.println(x.indexOf('a')); // 1
        System.out.println(x.indexOf('a',10)); //12
        System.out.println(x.indexOf(y)); //0
        System.out.println(x.indexOf(y, 13)); // -1
        System.out.println( x.indexOf(z) ); //16

        System.out.println("***************lastIndexOf()*****************");
        // lastIndexOf() 返回指定字符在字符串中最后一次出现的索引,如果没有返回-1
        System.out.println( x.lastIndexOf('a')); //14
        System.out.println( x.lastIndexOf( 'a', 10) ); //3
        System.out.println( x.lastIndexOf(y) ); // 11
        System.out.println( x.lastIndexOf(y, 8) ); // 0
        System.out.println( x.lastIndexOf(z) ); //16

        System.out.println("***************regionMatches()*****************");
        // 用于检测两个字符串在一个区域内是否相等。
        String Str11 = new String("www.runoob.com");
        String Str22 = new String("runoob");
        String Str33 = new String("RUNOOB");
        System.out.println(Str11.regionMatches(4, Str22, 0, 5)); //true
        System.out.println(Str11.regionMatches(4, Str33, 0, 5)); // false
        // ignoreCase :true 比较时忽略大小写
        System.out.println(Str11.regionMatches(true, 4, Str33, 0, 5)); //true

        System.out.println("***************replace()*****************");
        // str.replace(searchChar, newChar);
        System.out.println( Str11.replace('o', 'O')); // www.runOOb.cOm

        System.out.println("***************replaceAll()*****************");
        //replaceAll(regex, replacement) replacement替换所有匹配的的字符。// 失败返回原字符串

        System.out.println("***************replaceFirst()*****************");
        // replaceFirst();方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。

        System.out.println("***************split()*****************");
        // 根据匹配给定的正则表达式来拆分字符串。注意:. $ | * 等转义字符,必须加\\ ;多个分隔符,可以用 | 作为连字符。
        String sp = new String("welcome-to-java");
        for(String retval: sp.split("-")) {
            System.out.println(retval);
        }
        // 分隔成2份
        for(String retval: sp.split("-", 2)) {
            System.out.println(retval);
        }
        String sp2 = new String("www.java.com");
        for(String retval: sp2.split("\\.", 3)) {
            System.out.println(retval);
        }
        // | 连字符
        String sp3 = new String("name=? and uu =? or n=?");
        for(String retval: sp3.split("and|or")) {
            System.out.println(retval);
        }

        System.out.println("***************StringTokenizer类*****************");
        // 使用stringTokenizer设置不同分隔符来分隔字符串,默认分隔符是:空格、制表符、换行符、回车符
        String ss = "this is string, split by StringTokenizer, created by runoob";
        StringTokenizer st = new StringTokenizer(ss, ",");
        while(st.hasMoreElements()) {
            System.out.println( st.nextElement() );
        }

        System.out.println("***************format()*****************");
        // 使用format()方法来格式化字符串,

        System.out.println("***************reverse()*****************");
        // 将字符串反转reverse()
        String sr = "java";
        String ser = new StringBuffer(sr).reverse().toString();
        System.out.println(ser); // avaj
    }
}

你可能感兴趣的:(java 字符串实例练习)