String 类中常用的方法(一)

  1. 获取指定位置的字符
  2. 判断数组是否为空
  3. 转换大小写
  4. 将字符串首位部的空白省略输出
  5. 转换大小写
  6. 比较大小
  7. 截取指定位置的字符串
package StringClassUse;

import org.junit.Test;

/**
 * @Author:CT
 * @Date:2021/1/29
 * @Description:StringClassUse
 * @Version 1.0
 */
public class StringMethod {
    @Test
    public void test(){
        //获取指定位置的字符
        String s="Hello";
        System.out.println(s.charAt(2));

        //判断数组是否为空
       String  s1="";
        System.out.println(s1.isEmpty());

        //转换大小写
        System.out.println(s.toLowerCase());//修改为小写
        System.out.println(s.toUpperCase());//修改为大写 原字符串不变

        //忽略前导空白(首部尾部) 用处是输入用户名
        String s2="  he   llo";
        String s3=s2.trim();
        System.out.println(s3);


        //忽略大小写的比较方式
        System.out.println(s.equals(s1));
        System.out.println(s.equalsIgnoreCase(s));


        // 比较大小   就是两个字符串的ASCII值 相减
        String s4="abc";
        String s5="abe";
        System.out.println(s4.compareTo(s5));

        // 截取指定的字符串 sunstring 方法

        String s6="何似在人间";
        System.out.println(s6.substring(2));
        System.out.println(s6.substring(2,4));




    }
}

运行结果
l
true
hello
HELLO
he   llo
false
true
-2
在人间
在人

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值