demo1()的内存图解析: demo2()的内存图解析: demo5()的内存图解析: 6. 常见对象(String类的判断功能):
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写。boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写。boolean contains(String str):判断大字符串中是否包含小字符串boolean endsWith(String str):判断字符串是否以某个指定的字符串开头boolean isEmpty():判断字符串是否维空。注:""和null的区别: ""是字符串常量,同时也是一个String类的对象,既然是对象当然可以调用String类中的方法null是空常量,不能调用任何的方法,否则会出现空指针异常,但是null常量可以给任意的引用数据类型赋值。 常见对象(String类的获取功能) int length():获取字符串长度 int[] arr = {11, 22, 33}; System.out.println(arr.length);// 数组中的length是属性 String s1 = "haha"; System.out.println(s1.length()); char charAt(int index):获取指定索引位置的字符int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。int indexOf(int ch, int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。int indexOf(String str, int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。lastIndexof:从后向前找第一次出现的字符。String substring(int start):从指定位置开始截取字符串,默认到末尾。String substring(int start, int end):从指定位置开始到指定位置结束截取字符串。(左闭右开) 字符串的遍历 public class Test2_BianliString { public static void main(String[] args) { String s = "hello"; for (int i = 0; i < s.length(); i++ ) { System.out.println(s.charAt(i)); } } } 统计不同类型字符的个数 public class Test3 { public static void main(String[] args) { String s = "ABCDEabcd123456!@#$%^"; int big = 0; int small = 0; int num = 0; int other = 0; // 1. 判断每个字符,通过for遍历 for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); // 2. 判断字符是否在这个范围内 if (c >= 'A' && c <= 'Z') { big ++; } else if (c >= 'a' && c <= 'z') { small ++; } else if (c >= '0' && c <= '9') { num ++; } else { other ++; } } // 3.打印每个计数器的结果 System.out.println(s + "中大写字母有:" + big + "个,小写字母有:" + small + "个, 数字字符有:" + num + "个"); } } String类的转换功能byte[] getBytes():把字符串转换为字节数组
char[] toCharArray():把字符串转换为字符数组
static String valueOf(char[] chs):把字符数组转换为字符串
String类的valueOf方法可以把任意类型的数据转成字符串String toLowerCase():把字符串转成小写
String toUpperCase():把字符串转成大写
String concat(String str):把字符串拼接
public class Demo6_StringMethod { public static void main(String[] args) { //demo1(); //demo2(); //demo3(); String s1 = "helloWORLD"; String s2 = "HELLOworld"; System.out.println(s1.toLowerCase()); System.out.println(s2.toUpperCase()); } private static void demo3() { char[] arr = {'a', 'b', 'c'}; String s = String.valueOf(arr); // valueOf可以让任意类型数据转换为字符串 System.out.println(s); System.out.println(String.valueOf(100) + 100); } private static void demo2() { String s = "hello"; char[] arr = s.toCharArray(); // 把字符串转换为字符数组 for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } private static void demo1() { String s1 = "Aac"; byte[] arr = s1.getBytes(); for (int i = 0; i < arr.length; i ++) { System.out.print(arr[i] + " "); } } } 常见对象(按要求转换字符) 需求: 把一个字符串的首字母转换成大写,其余为小写。(只考虑英文大小写字母) public class Test4 { public static void main(String[] args) { String s = "heLLoWorlD"; String s1 = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase()); System.out.println(s1); } } 把数组转换成字符串 需求:把数组中的数据按照指定格式拼接成一个字符串 举例: 输入: int[] arr = {1, 2, 3};输出:"[1, 2, 3]" public class Test5 { public static void main(String[] args) { int[] arr = {1, 2, 3}; String s = "["; for (int i = 0; i < arr.length; i++) { if (i == arr.length - 1) { s = s + arr[i] + "]"; } else { s = s + arr[i] + ", "; } } System.out.println(s); } } String类的其他功能 A: String类的替换功能及案例演示 String replace(char old, char new)String replace(String old, String new) B: String的去除字符串两空格及案例演示: String trim() C: String的按字典顺序比较两个字符串及案例演示(了解) int compareTo(String str)int compareToIngnoreCase(String str) public class Demo7_StringMehtod { public static void main(String[] args) { //demo1(); //demo2(); } private static void demo2() { String s = " hel lo "; System.out.println(s.trim()); // 去除两边空格 } private static void demo1() { String s = "hello"; String s1 = s.replace('l', 'z'); System.out.println(s1); String s2 = s.replace('z', 'k'); // z不存在,保留源字符不改变 System.out.println(s2); } } 字符串反转 字符串反转 import java.util.Scanner; public class Test6 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); String line = sc.nextLine(); char[] arr = line.toCharArray(); // 将字符串转换为字符数组 String s = ""; for (int i = arr.length - 1; i >= 0; i--) { // 倒着遍历字符数组 s = s + arr[i]; // 拼接成字符串 } System.out.println(s); } } 常见对象(在长字符串中查找小字符串出现的次数) 题目:查找在长字符串中查找小字符串出现的次数 具体代码例子如下: public class Test7 { public static void main(String[] args) { // 定义长字符串 String max = "woaiheima, heimabutongyubaima,wulunheimahaishibaima,zhaodaogongzuojiushihaoma"; // 定义短字符串 String min = "heima"; // 定义计数器变量 int count = 0; // 定义索引 int index = 0; // 定义循环,判断短字符串是否在长字符串中出现 while((index = max.indexOf(min)) != -1) { count ++; max = max.substring(index + min.length()); } System.out.println(count); } } 注:本文整理自传智播客视频及其提供文档,手码,若有错误,希望大家指出(轻喷),共同学习。目的是便于个人复习,后来考虑到如果有其他同学也想复习,也想跟大家分享。不过毕竟本文整理自其他资料,若有侵权,请私信联系我,删。