String类是一个很重要的类,开发中经常用到,下面是我整理的常用的一些方法与如何使用
boolean equals(Object obj); 判断字符串的内容是否相等。
int length(); 获取字符串长度
char charAt(int index); 通过索引下标 获取当前下标的字符
int indexOf (String str);获取指定的字符第一个出现索引下标
int lastIndexOf(int ch);获取指定的字符最后一次出现索引下标
public class Demo1 {
public static void main(String[] args) {
//equals方法 判断字符串是否相等
String str1 = "abc";
String str2 = "abc";
/**
* str1==> char[] ch1 = ['a','b', 'c'];
* str2==> char[] ch2 = ['a', 'b', 'c']
* while循环 ch1[i] == ch2[i]
*/
System.out.println(str1.equals(str2));//true
System.out.println("abc".equals("ced"));//false
System.out.println("abcdef".length());//6
System.out.println("abcd".charAt(2));//c
System.out.println("bbccaa".indexOf("c"));//2
System.out.println("bbccaa".indexOf('c'));//2
System.out.println("abcdadf".lastIndexOf('a'));//4
}
}
返回值是布尔类型数据的方法
boolean endWith(String str);判断是否是以指定的字符或者字符串结尾的
boolean isEmpty(); 判断字符串是否为空
boolean contains(String str); 在一个字符串中是否包含另外一个字符串啊
boolean equalsIgnoreCase();忽略大小写比较字符串的是否相等
public class Demo2 {
public static void main(String[] args) {
String str1 = "Demo1.java";
System.out.println(str1.endsWith(".java"));//true
System.out.println(str1.endsWith("av"));//false
System.out.println("".isEmpty());//true
System.out.println("ab".isEmpty());//false
System.out.println(" ".isEmpty());//false
System.out.println("不破楼兰终不还".contains("不还"));//true
System.out.println("不破楼兰终不还".contains("还不"));//false
System.out.println("abc".equals("Abc"));//false
System.out.println("abc".equalsIgnoreCase("ABc"));//true
}
}
之后的几个方法比较重要,开发是要用的
将字符数组转为字符串
直接使用String类的构造方法
String(char[] ch);
static String valueOf(char[] chs);将字符数组转为字符串。静态的方法【重点】
valueOf方法的重载很多。一定要记住将八大基本数据类型转为字符串
将字符串转为字符数组
char[] tocharArray();将字符串转为字符数组【重点】
public class Demo3 {
public static void main(String[] args) {
char[] chs = {'a','g', 'e'};
//转为字符串
String str = new String(chs);
System.out.println(str);
String str1 = String.valueOf(chs);
System.out.println(str1);
String str2 = String.valueOf(98.76);
System.out.println(str2);//字符串类型的98.76
//一个double类型的数据 98.7654。取小数点后面的数
char[] chs1 = "我是狗蛋我怕谁".toCharArray();
System.out.println(chs1);
for (int i = 0; i < chs1.length; i++) {
System.out.println(chs1[i]);
}
}
}
String replace(char oldChar, char newChar);在字符串中用新的字符替换老的字符
String[] split(String regex); 以指定的字符串进行切割
String subString(int beginIndex);从指定的位置开始截取字符串
String subString(int beginIndex, int endIndex);从指定的位置开始截取字符串到指定的位置结束
String toUpperCase();将小写字符转为大写的
String toLowerCase();将小写字符转为大写的
String trim();去掉首尾空格
import java.util.Arrays;
public class Demo5 {
public static void main(String[] args) {
System.out.println("abcd".replace('a', '狗'));
System.out.println("abcd".replace("cd", "中国牛皮"));
//切割一个字符串
//["ab", "cd", "ef"]
//["a", ",cd,ef"]
String[] strs = "ab&cd&ef".split("&");
System.out.println(Arrays.toString(strs));
String str1 = "abcdef".substring(2);
System.out.println(str1);//cdef
//要头不要尾
String str2 = "abcdef".substring(2, 4);
System.out.println(str2);//cd
System.out.println("ava".toUpperCase());
System.out.println("ABDF".toLowerCase());
System.out.println(" gsi jsjj ");
System.out.println(" gsi jsjj ".trim());
}
}