String类的获取功能

String类的获取功能:(重点)

int length():获取字符串长度
public char charAt(int index);获取指定索引处的字符
public String concat(String str):将指定的字符串和当前字符串进行拼接,获取一个新的字符串
public int indexOf(int ch):返回指定字符第一次出现的索引值
public int lastIndexOf(int ch):返回值指定字符最后一次出现的索引值
public String[] split(String regex):
拆分功能:通过指定的格式将字符串—拆分字符串数组
public String substring(int beginIndex) :从指定位置开始默认截取到末尾
角标从0开始
public String substring(int beginIndex,int endIndex)
从指定位置开始,截取到位置结束(包前不包右)
只能取到endIndex-1处
public static String valueOf(boolean/int/long/float/double/char…Object b)
万能方法,将任意类型转换String类型

public class StringDemo {
    public static void main(String[] args) {

        String str = "helloworldJavaEE" ;
        // public char charAt(int index);获取指定索引处的字符
        System.out.println("charAt():"+str.charAt(4));
        System.out.println("---------------------------------");
        //public String concat(String str):将指定的字符串和当前字符串进行拼接,获取一个新的字符串
        //字符串最传统的拼接:直接  定义一个String s = "" ;  s+任何数据="xxx" ;
        //提供了一个功能
        System.out.println("concat:"+str.concat("R").concat("Go"));
        System.out.println("-----------------------------------");

        //   public int indexOf(int ch):返回指定字符第一次出现的索引值
        // public int lastIndexOf(int ch):返回值指定字符最后一次出现的索引值
        System.out.println("indexOf():"+str.indexOf("o"));
        System.out.println("lastIndexOf():"+str.lastIndexOf("o"));
        System.out.println("-----------------------------------");
        //public String[] split(String regex):
        String str2 = "JavaEE-Python-Go-R-C-C#-PHP" ;
        //使用"-"进行拆分
        String[] strArray = str2.split("-");
        for(int x = 0 ; x < strArray.length ; x ++){
            System.out.print(strArray[x]+"\t");
        }
        System.out.println();
        System.out.println("-----------------------------------");
        /**
         *  public String substring(int beginIndex) :从指定位置开始默认截取到末尾
         *                                    角标从0开始
         *  public String substring(int beginIndex,int endIndex)
         */
        // String str = "helloworldJavaEE" ;
        System.out.println("subString():"+str.substring(5));
        System.out.println("subString():"+str.substring(5,9));//worl
        System.out.println("-----------------------------------");
        //public static String valueOf(基本类型以及Object)
        System.out.println("valueOf:"+String.valueOf(100)); //100--->"100"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值