字符串的常用方法

import java.util.Arrays;

public class List1<T> {
    public static void main(String[] args) {
        //字符串常营方法
        String str="hello world";
        String str1="hello world";
        String str2="HELLO world";
        String str3="   hello world887    ";
        System.out.println(str.length());//输出字符串长度,空格也算    11
        System.out.println(str.equals(str1));//判断两个字符串内容是否相等       true
        System.out.println(str.equalsIgnoreCase(str2));//忽略两个字符串的大小写并判断是否相等  true

        System.out.println(str3.trim());//清除字符串两边的空格   hello world887
        System.out.println(str.substring(2));//从第二位开始截取字符串输出包括第二位到最后的字符串   llo world

        System.out.println(str.substring(2,7));//截取第二位到第七位的字符串。包括第二位不包括第七位   llo w

        System.out.println(str1.concat(str2));//将字符串str2续到str1的后边并输出续后的结果    hello worldHELLO world

        System.out.println("www".concat(str2));//同上   wwwHELLO world
        System.out.println(str2.contains("HE"));//判断字符串str2是否包含HE      true
        System.out.println(str1.indexOf("l"));//从头开始找返回字符l的位置   2
        System.out.println(str1.lastIndexOf("l"));//从末尾开始找返回l的位置   9
        System.out.println(str3.replaceAll("\\d",""));//替换正则表达式的值为空     (   hello world)

        System.out.println(str3.replace("l","q"));//替换字符串str3中的l变为q      (   heqqo worqd887)

        String []st;
        st=str.split("l");//将字符串str按照l分开为字符串数组
        System.out.println(Arrays.toString(st));//返回分开的字符串数组   [he, , o wor, d]
        System.out.println(st.length);//返回字符串数组的长度    4
        System.out.println(str2.toLowerCase());//将字符串str2全部转换为小写字母   hello world

        System.out.println(str.toUpperCase());//将字符串str全部转换为大写字母    HELLO WORLD
        System.out.println("s".repeat(3));//将字符串s重复3次    sss
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值