String的常用工具类

String的常用工具类

package Jobdemo08.DemoTest;
/* 
创建字符串对象     
String s = "helloworld";       
int length():获取字符串的长度,其实也就是字符个数     
*/
public class DemoTest2 {
    public static void main(String[] args) {
        String s="helloworld";

//1length() 获取字符串的长度

//s字符串长度为:10

System.out.println("s字符串长度为:"+s.length());

 

//2String concat (String str):将将指定的字符串连接到该字符串的末尾.     
String s2=s.concat("**hello itheima");

 //新的字符串s2:helloworld**hello itheima

System.out.println("新的字符串s2:"+s2);


// 3char charAt(int index):获取指定索引处的字符     

 System.out.println(s.charAt(0));//h
 System.out.println(s.charAt(1));//e
 System.out.println("‐‐‐‐‐‐‐‐");


        //4int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
        System.out.println(s.indexOf("owo"));//4
        System.out.println(s.indexOf("ld"));//8
        System.out.println(s.indexOf("lwlw"));//-1
        System.out.println("‐‐‐‐‐‐‐‐");


        //5String substring(int start):从start开始截取字符串到字符串结尾
        System.out.println(s.substring(1));//elloworld
        System.out.println(s.substring(4));//oworld
        System.out.println("‐‐‐‐‐‐‐‐");


         //6String substring(int start,int end):从start到end截取字符串。含start,不含end
        System.out.println(s.substring(2,5));//llo
        System.out.println(s.substring(4,8));//owor
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值