String类

1.lenth()方法

length():确定字符串的长度,返回字符串中的字符数

2. equals()方法

equals():比较存储在两个字符串对象的内容是否一致
字符串比较其他方法:
equalsIgnoreCase()方法:忽略大小写比较
toLowerCase()方法:将字符串中的字符转换为小写字符
toUpperCase()方法:将字符串中的字符转换为大写字符

3. 搜索字符串

indexOf(String value):返回第一个出现的字符串value的下标,如果没有找到则返回 -1
lastIndexOf(String value):返回最后一个出现的字符串value的下标,如果没有找到则返回 -1

4.提取字符串

substring(int i):返回下标 i (包括i)往后的所有字符串
substring(int i1,int i2):返回下标 i1 到下标 i2 的字符串(包前不包后)

5.去空白

trim():去除字符串首尾的空白

6.拼接字符串

字符串1 + 字符串2
concat()方法

public static void main(String[] args) {
	String str1 = "abcdefg";
	System.out.println(str1.length());  // 求字符串长度
	String str2 = "abcdefg";· 
	System.out.println(str1.equals(str2));  // 判断字符串是否相同
		
	String str3 = "abcdEF";
	String str4 = "aBcDEF";
	System.out.println(str3.equals(str4));//比较字符串是否相等
	System.out.println(str3.equalsIgnoreCase(str4));//比较字符串是否相等(不分大小写)
		
	String str5 = "abcdefg";
	System.out.println(str5.toUpperCase());//将字符串全部转换成大写
	System.out.println(str3.toLowerCase());////将字符串全部转换成小写
		
	String str6 = "abcdabcd";
	System.out.println(str6.indexOf("abc"));//返回第一个出现的字符串value的下标,如果没有找到则返回 -1
	System.out.println(str6.indexOf("z"));
	System.out.println(str6.indexOf(98));
		
	String str7 = "中国";
	System.out.println(str7.indexOf(20013));
	System.out.println(str6.lastIndexOf("a"));//返回最后一个出现的字符串value的下标,如果没有找到则返回 -1
	System.out.println(str6.lastIndexOf(97));
		
	String str8 = "abcdefhiklmn";
	System.out.println(str8.substring(3));//返回下标 i (包括i)往后的所有字符串
	System.out.println(str8.substring(3,6)); // 返回下标 i1 到下标 i2 的字符串(包前不包后)
		
	String str9 = "		dsajhdoqwererwq ds wqe qw ewqe wqe wqe wq ew     ";
	System.out.println(str9.trim()); // 去除的首尾的空白
		
	String str10 = "a";
	String str11 = "b";
	System.out.println( str10 + str11);
	System.out.println(str10.concat(str11));
}

6.拆分字符串

split():将拆分后的字符串,返回一个char数组

public static void main(String[] args) {
	String song = "长亭外 古道边 芳草碧连天 晚风拂柳 笛声残 夕阳山外山";
	String [] strs = song.split(" ");
	for (int i = 0; i < strs.length; i++) {
		System.out.println(strs[i]);
	}
}

查找指定字符出现咋次数

public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	System.out.println("请输入一句话");
	String str = input.next();
	System.out.println("请输入要查找的字符");
	String findStr = input.next();
	String [] strs = str.split("");
	int count = 0;
	for (int i = 0; i < strs.length; i++) {
		if(strs[i].equals(findStr)) {
			count++;
		}
	}
	System.out.println("一共有" + count + "个" + findStr);	
}

7.判断字符串是否以某一个字符串开头或者结尾

endsWith() 判断字符串是否以某一个字符串结尾
startsWith() 判断字符串是否以某一个字符串开头

public static void main(String[] args) {
	String str1 = "abcdefg";
	System.out.println(str1.startsWith("f"));
	System.out.println(str1.endsWith("abcdefg"));
}

8.更多方法

charAt(int index) 返回指定位置的字符
toCharArray() 将字符串转换为char数组
replace(String oldStr,String newStr) 替换指定内容的字符串
replaceAll(String regx,String newStr) 支持正则表达式的字符串替换
isEmpty() 判断是否为null 长度为0 返回为true 否则false

public static void main(String[] args) {
	String str1 = "abcdefg";
	System.out.println(str1.charAt(3));//返回指定位置的字符
	char[] charArray = str1.toCharArray();//将字符串转换为char数组
	for (int i = 0; i < charArray.length; i++) {
		System.out.println(charArray[i]);
	}
	String str2 = "abcdefabcdef";
	System.out.println(str2.replace("a", "A"));
	String str3 = "1a1b1c1d1e1f";
	System.out.println(str3.replace("1", "hello"));
	String str4 = "1a2b3c4d5e6f";
	System.out.println(str4.replaceAll("\\d", "Hello"));
		
	String str5 = "  sasa we  rt  v ty ytr vfd d   ";
	System.out.println(str5.replace(" ", ""));
	String str6 = " 	ewewq	 ewqewq				ewqewqewq vdssfdsgfsd cfdfsdw		    rerrew  ";
	System.out.println(str6.replaceAll("\\s", ""));
		
	// 如果出现空指针异常 
	// 表示肯定访问了一个指向null(空的)的引用 调属性或者方法
	String str7 = new String();
	System.out.println(str7.length());
	System.out.println(str7.isEmpty());		
}

9.面试题

String类是一个不可变对象 因为String类底层维护的是一个final修饰的char数组,任何对原字符串进行的增删改操作,都将产生一个新的字符串,而StringBuffer和StringBuilder都是在原字符串上进行操作,不会产生新的字符串
StringBuffer StringBuilder 区别:
StringBuffer是线程安全的 JDK1.0
StringBuilder线程不安全 JDK1.5
String、StringBuffer、StringBuilder 的区别
String类是一个不可变对象
StringBuffer和StringBuilder是可变对象

public static void main(String[] args) {
	StringBuilder sb = new StringBuilder();
	sb.append(false);  // 在字符串后插入false
	System.out.println(sb.length());  // 求字符串长度
	sb.append("hello world");
	System.out.println(sb.charAt(3));  // 求下标为3的字符
	System.out.println(sb.delete(0, 5));  // 删除 0-5 下标的字符(包前不包后)
	sb.insert(3, "666");  //  在下标3后插入666
	System.out.println(sb);
	sb.replace(0, 3, "abcd");  // 将下标 0-3 替换为 abcd
	System.out.println(sb);
	sb.reverse();  // 字符串反转
	System.out.println(sb);	
}
posted @ 2021-07-24 15:04  码丁XIA  阅读(43)  评论(0)    收藏  举报