package com.qianfeng.周六;
public class StringTest1 {
public static void main(String[] args) {
String i= "adsacgfbcerbhd";
//根据下标获取字符
System.out.println(i.charAt(2));
System.out.println(i.charAt(5));
//查看数组中是否包含其中的字符串
System.out.println(i.contains("nc"));//boolean类型
System.out.println(i.contains("dsg"));//boolean类型
//将字符串转换为数组并遍历出来
char[] ch = i.toCharArray();
for (int j = 0; j < ch.length; j++) {
System.out.print(ch[j]);
}
System.out.println();
//查找i中首次出现的下标,存在则返回该下标,不存在,则返回-1
System.out.println(i.indexOf('c'));
System.out.println(i.indexOf("gfb"));
System.out.println(i.indexOf("a",5));//从下标5开始查字符a,后面没有字符a,所以就返回-1
//查找字符串在当前字符串最后一次出现的下标索引
System.out.println(i.lastIndexOf('b'));
System.out.println(i.lastIndexOf('g'));
//返回字符串的长度
System.out.println(i.length());
//去掉字符串的空格位置
String st = " hello world";
System.out.println(st);
System.out.println(st.trim());
//将小写转换成大写
System.out.println(i.toUpperCase());
System.out.println(st.trim().toUpperCase());
//将大写转换成小写
String str = "DSJFNJMK";
System.out.println(str.toLowerCase());
//字符串比较大小(字符串比较的是内容)
String s1 = "chfaFHRJHF";
String s2 ="chfafhrjhf";
System.out.println(s1.equals(s2));//比较的是两个字符串的内容,区分大小写
System.out.println(s1.equalsIgnoreCase(s2));//equalsIgnoreCase()忽略大小写比较
//判断字符串是否是以某个字符结尾
String c = "Helloworld.java";
System.out.println(c.endsWith(".java"));//判断是否以.java结束
System.out.println(c.startsWith("hello"));//判断是否以hello开头的,区分大小写
System.out.println(i.startsWith(c, 'b'));
//将旧字符串替换为新字符串
String d = "aaa";
String b = d.replace('a', 'b');//可以更换字符
System.out.println(b);
System.out.println(d.replace("aaa", "abc"));//也可以更换字符串
//根据字符串中的某个内容分割
String str1 = "人生就像一场戏,因为有缘才相遇,相扶到老不容易,是否更该去珍惜";
String[] split = str1.split(",");//这里和内容里面都需要英文符号
for (int j = 0; j < split.length; j++) {
System.out.println(split[j]);
}
}
}
String类里面的常用方法
最新推荐文章于 2024-09-08 10:40:56 发布