Java 基础(String的常用方法)

时间:2021-05-24 05:35:10   收藏:0   阅读:0

注: indexOf 和 lastIndexOf 方法如果未找到都是返回-1

package com.klvchen.java;

import org.junit.Test;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import java.util.Locale;

public class myTest {

    @Test
    public void test4(){
        String str1 = "中国教育出版社";
        String str2 = str1.replace("中国", "广州");
        System.out.println(str1);  //中国教育出版社
        System.out.println(str2);  //广州教育出版社

        System.out.println("***********************************");
        String str = "12hello34world5java7891mysql3456";
        String string = str.replaceAll("\\d+", ",").replaceAll("^,|,$", "");
        System.out.println(string); //hello,world,java,mysql

        str = "12345";
        // 判断str字符串中是否全部由数字组成,即有1-n个数字组成
        boolean matches = str.matches("\\d+");
        System.out.println(matches);  //true
        //判断这是否是一个杭州的固定电话
        String tel = "0571-12345678";
        boolean result = tel.matches("0571-\\d{7,8}");
        System.out.println(result);   //true

        System.out.println("***********************************");
        str = "hello|world|java";
        String[] strs = str.split("\\|");
        for (int i = 0; i < strs.length; i++){
            System.out.println(strs[i]);
        }
        System.out.println();
        str2 = "hello.world.java";
        String[] strs2 = str2.split("\\.");
        for (int i = 0; i < strs2.length; i++ ){
            System.out.println(strs2[i]);
        }

    }

    @Test
    public void test3(){
        String str1 = "helloworld";
        boolean b1 = str1.endsWith("ld");
        System.out.println(b1);   //true

        boolean b2 = str1.startsWith("He");
        System.out.println(b2);   //false

        boolean b3 = str1.startsWith("ll", 2);
        System.out.println(b3);   //true

        String str2 = "wo";
        System.out.println(str1.contains(str2)); //true

        System.out.println(str1.indexOf("lol"));  //-1
        System.out.println(str1.indexOf("lo",5)); //-1

        String str3 = "helloworld";

        System.out.println(str3.lastIndexOf("or"));  //6
        System.out.println(str3.lastIndexOf("or", 6)); //6

    }

    @Test
    public void test2(){
        String s1 = "HelloWorld";
        String s2 = "helloworld";
        System.out.println(s1.equals(s2));   //false
        System.out.println(s1.equalsIgnoreCase(s2)); //true

        String s3 = "abc";
        String s4 = s3.concat("def");
        System.out.println(s4);           //abcdef

        String s5 = "abc";
        String s6 = new String("abe");
        System.out.println(s5.compareTo(s6)); // -2, 涉及字符串排序

        String s7 = "中国教育出版社";
        String s8 = s7.substring(2);
        System.out.println(s7);  // 中国教育出版社
        System.out.println(s8);  // 教育出版社
    }

    @Test
    public void test1() {
        String s1 = "hellowoRld";
        System.out.println(s1.length());   //10
        System.out.println(s1.charAt(0));  //h
        System.out.println(s1.charAt(9));  //d

        System.out.println(s1.isEmpty());  //false

        String ss = "";
        System.out.println(ss.isEmpty());  //true

        String s2 = s1.toLowerCase();
        System.out.println(s1);            //hellowoRld
        System.out.println(s2);            //helloworld

        String s3 = "   he   llo   world   ";
        String s4 = s3.trim();
        System.out.println("----------" + s3 + "------------"); //----------   he   llo   world   ------------
        System.out.println("----------" + s4 + "------------"); //----------he   llo   world------------

    }
}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!