String类的常用方法

/*
String 类中常用的方法
 1. indexOf();根据字符返回字符串的索引(即字符串中对应字符的下标)
 2. charAt(); 根据索引返回字符(通过字符串的下标找出对应的字符)
 3. replace("旧的字符串","新的字符串"); 将字符串中旧的字符串替换成新的字符串
 4. trim(); 去除字符串两端的空白
 5. toUpperCase();字符串转换大写;toLowerCase()转换为小写
 6. split();字符串分割,返回的是数组
 7.substring(int beginIndex);这个的作用为截取从beginindex位置处的元素开始,默认截取至剩余所有。
   substring(int beginIndex, int endIndex);这个的作用为截取从beginIndex开始,截取至endIndex-1位置间的元素
 */

import org.w3c.dom.ls.LSOutput;

public class Demo2 {
    public static void main(String[] args) {
        String str = "  adergv btyytr  ";
        //1.indexOf()
        System.out.println(str.indexOf('y'));
        //2.charAt();
        System.out.println(str.charAt(3));
        //3.replace()
        System.out.println(str.replace("ad","da"));
        //4.trim()
        System.out.println(str);
        System.out.println(str.trim());
        //5.toUpperCase(),toLowerCase()
        System.out.println(str.toUpperCase());
        str.toUpperCase();
        System.out.println(str.toLowerCase());
        //6.split
        String[] arr = str.split(" ");//根据空格分割
        for ( String s:
             arr) {
            System.out.println(s);
        }
        System.out.println(arr.length+","+arr[0]);
        //7.substring()
        System.out.println(str);
        System.out.println(str.substring(2));//substring(int beginIndex)
        System.out.println(str.substring(3,5));//substring(int beginIndex, int endIndex)
    }



}

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值