javaAPI(1)之String类的常见操作

这里写图片描述
简单使用:

public class APITest {
    public static void main(String[] args){
        String name="Machin.echjidhu ";
        System.out.println("字符串的长度为:"+name.length());
        System.out.println("字符串是否为空:"+name.isEmpty());
        System.out.println("下标为5的字符是:"+name.charAt(5));
        System.out.println("字符串是否为          \"Machine\":"+name.equals("Machine"));
        System.out.println("c字符第一次出现的下标是:"+name.indexOf('c'));
        System.out.println("c字符第一次出现的下标是:"+name.lastIndexOf('c'));
        System.out.println("字符串是否以\"Mac\"开头"+name.startsWith("Mac"));
        System.out.println("字符串是否为hu结束"+name.endsWith("hu"));
        System.out.println("字符是否包含:"+name.contains("ech"));
        System.out.println("将字符串变成大写:"+name.toUpperCase());
        System.out.println("将字符串变成小写:"+name.toLowerCase());
        System.out.println("返回一个字符串:"+String.valueOf(89));
        char[] oe = name.toCharArray();
        System.out.print("字符串转化为字符数组后为:");
        for(int i=0;i<name.length();i++){
            if(i!=name.length()-1){
                System.out.print(oe[i]+",");
            }
            else{
                System.out.print(oe[i]+"\n");
            }
        }
        System.out.println("字符串中的ine用eni代替:"+name.replace("ine", "eni"));
        String[] a=name.split("\\.");
        System.out.println("字符串分割成.左右的字符串:"+a[0]+","+a[1]);
        System.out.println("返回下标值4及后面的字符串"+name.substring(4));
        System.out.println("返回下标值4后面(包括4)9前面(不包括9)的字符串"+name.substring(4,9));
        System.out.println("去除首尾空格的字符串:"+name.trim());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值