首页 > 其他分享 >String字符串类等

String字符串类等

时间:2022-11-29 19:34:16浏览次数:49  
标签:String System println 字符串 out string string1

package com.javasm.demo;
import java.util.Arrays;
import java.util.Date;
/**
 * @Author:liulei
 * @Version:1.0
 * @Date:2022/11/29-15:54
 * @Since:jdk1.8
 * @Description:
 */
public class StringDemo2 {
    public static void main(String[] args) {
        String string1 = "Aello你好123123";
        String string2 = "Cello123";
        //比较
        System.out.println(string1.compareTo(string2));//  比较A-C-->-2
        System.out.println("是否以h开头:" + string1.startsWith("h"));//false
        System.out.println("是否以h结尾:" + string1.endsWith("h"));//false
        //字符串搜索:索引位置
        System.out.println("查找索引位置:" + string1.indexOf("A"));//0
        System.out.println("查找索引位置,开始查找的位置:" + string1.indexOf("A", 1));//-1
        System.out.println("传递ascll值,查找对应的索引位置:" + string1.indexOf(49));//7
        System.out.println("传递ascll值,查找对应的索引位置:" + string1.indexOf(49, 6));//7
        System.out.println("查找最后一个索引位置:" + string1.lastIndexOf("a"));//-1
        //提取字符串
        System.out.println("提取索引1位置的字符:" + string1.charAt(1));//e
        //从5截取到最后
        System.out.println("截取字符串:" + string1.substring(7));//从索引7开始-->123123
        //截取,不包含结束索引!!!
        System.out.println("截取字符串,起始索引和结束索引:" + string1.substring(5, 7));//索引5-7-->你好
        //拼接字符串
        string1 = string1.concat("社发斯蒂芬");
        string1 = string1.concat(new Date().toString());
        System.out.println(string1);//Aello你好123123社发斯蒂芬Tue Nov 29 19:13:48 CST 2022
        //替换
        string1 = string1.replace('e', 'E');
        System.out.println(string1);//AEllo你好123123社发斯蒂芬TuE Nov 29 19:13:48 CST 2022
        //替换字符串
        string1 = string1.replaceAll("123", "321");
        System.out.println(string1);//AEllo你好321321社发斯蒂芬TuE Nov 29 19:14:29 CST 2022
        //去空格
        String string = " 123 456 789 ";
        System.out.println(string);// 123 456 789 
        System.out.println(string.trim());  //123 456 789去头尾的空格
        //将所有的空格替换
        string = string.replaceAll(" ", "");
        System.out.println(string);//123456789
        //分割字符串
        string = "123,456,789,abc";
        String[] split = string.split(",");
        System.out.println(Arrays.toString(split));//[123, 456, 789, abc]
        System.out.println(split.length);//4
        //判断是否包含?
        System.out.println("是否包含:" + string.contains("123"));//true
        String isContinue = "Y";
        if (isContinue.toLowerCase().equals("y")) {
            System.out.println("OK!");
        }//输y和Y都可以输出OK
        /*
            通过string+byte实现编码格式的转换!
            getBytes:将字符串转成byte数组!
         */
        String string3 = "hello你哈";
        System.out.println(Arrays.toString(string3.getBytes()));
        //[104, 101, 108, 108, 111, -28, -67, -96, -27, -109, -120]

        /*
            将字符串转成字符数组!  char []
         */
        char[] chars = string3.toCharArray();
        System.out.println(Arrays.toString(chars));//[h, e, l, l, o, 你, 哈]
        /*
            将“123”转成 123!
         */
        String string4 = "123";
        char[] chars1 = string4.toCharArray();
        for (int c : chars1) {
            System.out.println(c - 48);
        }
    }
}

 

标签:String,System,println,字符串,out,string,string1
From: https://www.cnblogs.com/19981206-zxb/p/16936468.html

相关文章