package com.wq.String_.String常用方法;
public class String常用方法1 {
public static void main(String[] args) {
//1. equals 比较内容是否相等 区分大小写
String s = "hello";
String s1 = "Hello";
System.out.println(s.equals(s1));//f
//2. equalsIgnoreCase 忽略大小写 判断内容是否相等
String username = "king";
if("KING".equalsIgnoreCase(username)){
System.out.println("Success");
}else{
System.out.println("Failure");
}
//3. length 获取字符的个数 字符串的长度
System.out.println("king".length());
//4. indexOf 获取字符在字符串对象中第一次出现的索引, 索引从0开始
//如果找不到 返回 -1
String ss = "asdfgh#$%";
int index = ss.indexOf('%');
System.out.println(index);// 8
int index2 = ss.indexOf("$%");
System.out.println("index2 =" + index2);
//5. lastIndexOf 获取字符在字符串中 最后一次出现的索引 索引从0开始 如果找不到
// 返回 -1
String 是是是 = "p555oiuyt52";
int index1 = 是是是.lastIndexOf('5');
System.out.println(index1);
//6. substring 截取指定字符串
String name = "king,国王";
System.out.println(name.substring(5));//从索引5 开始截取 要后面的字符串
System.out.println(name.substring(0,4));// 从索引 0 开始截取 截取到 4-1 个索引的位置
}
}
java String常用方法1
最新推荐文章于 2024-07-15 22:27:12 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)