public class StringDemo {
public static void main(String[] args) {
String str = "abcd";
//字符串长度
int len = str.length();
//查找字符串索引位置上的数
char ch = str.charAt(2);
//查找字符串第一次出现查找字符的索引
int index = str.indexOf('a');
//判断开头是否包括查找的字符串
boolean s = str.startsWith("a0");
//判断结尾是否包括查找的字符串
boolean e = str.endsWith("a");
//判断是否包括查找的字符串
boolean con = str.contains("a");
//获取字符串中的一部分
String substr = str.substring(1,3);
//替换字符串
String rep = str.replace("ab"," abshd ");
//去掉两端的空格
rep.trim();
//比较字符串大小
int num = str.compareTo("sdc");
System.out.println(replace);
}
}
一分钟快速了解Java中String类的基本方法
最新推荐文章于 2024-09-14 18:46:05 发布