String类的常用方法

package com.atguigu.java;

import org.junit.Test;

/**
 * @author Suki
 * @creat 2022-09-16 19:10
 */
public class StringMethodTest {

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

        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 = "abcdefg";
        String s8 = s7.substring(2);
        System.out.println(s7);//abcdefg
        System.out.println(s8);//cdefg

        String s9 = s7.substring(2,5);
        System.out.println(s9);//cde  [2,5)左开右闭
    }

    @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(10));//角标越界
        System.out.println(s1.isEmpty());//false--判断数组长度

        String s2 = s1.toLowerCase();
        System.out.println(s1);//HelloWorld  s1不可变的
        System.out.println(s2);//helloworld  改成小写以后的字符串

        String s3 = "   he  l lo  wor  ld     ";
        String s4 = s3.trim();//去除首位空格
        System.out.println("-----" + s3 + "-----");
        System.out.println("-----" + s4 + "-----");



    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值