java 之String类

String基础知识:

package com.gongxy.demo;

import java.util.Arrays;

/**
 * String 类的基础
 */
public class StringTest {
    public static void main(String[] args) {
        testMethod5();
    }

    /**
     * char 与 String 互转
     */
    static void testMethod1(){
        String sParam1 = "abc123";
        char[] cParam1 = sParam1.toCharArray();
        for (char c:
             cParam1) {
            System.out.println(c);
        }

        char[] cParam2 = new char[]{'a','b','c'};
        String sParam2 = new String(cParam2);
        System.out.println(sParam2);
    }

    /**
     * 字符串比较
     */
    static void testMethod2(){
        String sParam1 = "abc";
        String sParam2 = new String("abc");
        System.out.println(sParam1==sParam2);//false
        System.out.println(sParam1.equals(sParam2));//true
        System.out.println(sParam1.endsWith("c"));//true
        System.out.println(sParam1.startsWith("b"));//false
        String sParam3 = "Abc";
        System.out.println(sParam1.equals(sParam3));//false
        System.out.println(sParam1.equalsIgnoreCase(sParam3));//true  忽略大小写比较
    }

    /**
     * 查找字符
     */
    static void testMethod3(){
        String sParam1 = "ab1cde1fg123";
        System.out.println(sParam1.indexOf("1"));//2
        System.out.println(sParam1.indexOf("h"));//-1
        System.out.println(sParam1.lastIndexOf("1"));//9
        System.out.println(sParam1.charAt(4));//d
        System.out.println(sParam1.charAt(sParam1.length()-1));//3
        //System.out.println(sParam1.charAt(14));//java.lang.StringIndexOutOfBoundsException
    }

    /**
     * 字符串处理
     */
    static void testMethod4(){
        String sParam1 = " abCDe1fg ";
        System.out.println(sParam1.substring(1));//abCDe1fg
        System.out.println(sParam1.substring(1,3));//ab
        System.out.println(sParam1.trim());//abCDe1fg
        System.out.println(sParam1.toUpperCase());// ABCDE1FG
        System.out.println(sParam1.toLowerCase());// abcde1fg
        String sParam2 = "192-168-1-1";//用.的时候有别的问题【需要转义("\\.")】
        String[] sArrParam1 = sParam2.split("-");
        System.out.println(Arrays.toString(sArrParam1));//[192, 168, 1, 1]
    }

    /**
     * 字符串构建
     */
    static void testMethod5(){
        String sParam1 = "abc" + "c";//这是两个字符串拼接成的第三个字符串
        //StringBuffer sb = new StringBuffer();//一般多线程使用
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < 10; i+=2) {
            sb.append(i).append(",");
        }
        System.out.println(sb);
        sb.deleteCharAt(sb.length()-1);
        System.out.println(sb);
        sb.delete(1, 4);//删除第一个字符到第4个字符,截止下标对应的字符不会被删除
        System.out.println(sb);
        sb.replace(0, 2, "a");//从指定位置到结束位置前一个字符替换为指定的字符
        System.out.println(sb);
        sb.setCharAt(2, 'n');//替换一个
        System.out.println(sb);
        String sParam2 = sb.toString();
        System.out.println(sParam2);
    }
}

  

posted @ 2020-08-09 00:12  gygtech  Views(156)  Comments(0Edit  收藏  举报