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
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容