Java的String类 Li.033
Scanner hasNext() 表示判断下一个输入项是否是字符串, hasNextInt() 表示判断下一个输入项是否是int, 否则返回false next()获取输入项的字符串, nextInt 获取项输入的int import java.util.Scanner; public class Demo1_Scanner { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); if( scanner.hasNextInt()) { int p = scanner.nextInt(); System.out.println(p); } else { System.out.println("输入内容不是int 数字"); } scanner.close(); } } 输入1个整数, 1个数字串, 输出数字和字符串, 错误示例 import java.util.Scanner; public class Demo2_Scanner { public static void main(String[] args) { // 输入1个整数, 1个字符串的 错误示例 Scanner sc = new Scanner(System.in); int i = sc.nextInt(); String s = sc.nextLine(); System.out.println(i+" "+s); } } 输入1个整数, 1个数字串, 输出数字和字符串, 正确示例, 都当成一行字符串, 后期String转成int类型 import java.util.Scanner; import java.lang.Integer;; public class Demo2_Scanner { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); int i = Integer.parseInt(s1); String s2 = sc.nextLine(); System.out.println(s1+ s2); } } String 类 toString()方法, 没重写前显示包和类名@hashcode() 常量池的string, 没有则创建, 有则使用现有的string 使用new String()则会创建常量的副本 public class Demo2_String { public static void main(String[] args) { String s1 = new String("abc"); String s2 = "abc"; System.out.println(s1 == s2); System.out.println(s1.equals(s2)); } } //结果 false ture 使用+号连接的, 是在StringBuffer中连接后, 再把地址指给变量 public class Demo2_String { public static void main(String[] args) { String s4 = "ab"; String s5 = s4 + "c"; System.out.println(s4 == s5); } } // false String 其他方法 startWith endWith equalsIgnoreCase contains public class Demo3_String { public static void main(String[] args) { String sm = "123 sdf z"; String s1 = "123"; String s2 = "z"; String s3 = "123 SDF z"; String s4 = "sdf"; String s5 = "sDf"; System.out.println(sm.startsWith(s1)); System.out.println(sm.endsWith(s2)); System.out.println(sm.equalsIgnoreCase(s3)); System.out.println(sm.contains(s4)); System.out.println(sm.contains(s5)); System.out.println(sm.toLowerCase().contains(s4.toLowerCase())); } } // T T T T F T String练习, 模拟用户登录 import java.util.Scanner; public class Demo4_String { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = ""; String passwd = ""; for(int i=0;i<3;i++){ System.out.println("请输入用户名:"); if( sc.hasNextLine() ){ name = sc.nextLine(); } System.out.println("请输入用户密码:"); if( sc.hasNextLine() ){ passwd = sc.nextLine(); } if( "admin".equals(name) && "admin".equals(passwd) ){ System.out.println("欢迎"+ name + "登录!"); break; } else { if( (2-i)==0 ){ System.out.println("请找回密码, 或过1小时后再重试 !"); } else{ System.out.println("用户名或密码错误, 您还有" + (2-i) + "次机会!"); } } name = ""; passwd = ""; } sc.close(); } } String 的方法 char charAt(int index) 获取指定索引位置的字符 int length() 获取字符串长度 int indexOf(int ch) 获取字母在字符串第一次出现的索引 int indexOf(String str) 获取字符串第一次出现的出现的索引位置 int indexOf( int ch, int fromIndex ) 从指定索引位置向后, 字母ch 第一次出现的索引位置 int indexOf( String str, int fromInex ) 从指定索引位置向后, str 第一次出现的索引位置 int lastIndexOf(int ch) 最后一次出现字符的索引, 即从后向前找第1次出现字母的索引 int lastIndexOf(int ch, int fromIndex) 从指定索引向前找, 字素ch 出现的索引 int lastIndexOf(String str) int lastIndexOf(String str, int fromIndex) String substring(int beginIndex) 从索引位置开始到结尾截取字符串 String substring(int beginIndex, int endIndex) 从开始索引 到 结束索引位置前(结束索引不包含) 截取字符串 public class Demo5_String { public static void main(String[] args) { String sm = "Hello Wrold!"; System.out.println(sm.charAt(0)); //System.out.println(sm.charAt(20)); System.out.println(sm.length()); System.out.println(sm.indexOf('l')); System.out.println(sm.indexOf('z')); System.out.println(sm.indexOf("Wrold")); System.out.println(sm.indexOf("Wrood")); System.out.println(sm.indexOf('o',5)); System.out.println( sm.indexOf( "old", 5 ) ); } } public class Demo6_String { public static void main(String[] args) { String s = "ET drive UFO! So COOL"; System.out.println(s.lastIndexOf('O')); System.out.println(s.lastIndexOf('O',17)); System.out.println(s.lastIndexOf("UFO")); System.out.println(s.lastIndexOf("So", 9)); } } public class Demo7_String { public static void main(String[] args) { String s = "PhpIsBest?"; System.out.println(s.substring(3)); System.out.println(s.substring(0,3)); } } String 练习, 统计大写, 小写, 数字, 其他字符个数 public class Demo9_String { public static void main(String[] args) { String s = "C C++ C# Go Java Javascript Php Swift 2021"; int sumUp = 0; int sumLow = 0; int sumNu = 0; int sumOther = 0; for(int i=0;i<s.length();i++){ char tmpChar = s.charAt(i); if( tmpChar <= 'z' && tmpChar >= 'a'){ sumLow++; } else if( tmpChar <= 'Z' && tmpChar >= 'A' ){ sumUp++; } else if( tmpChar <= '9' && tmpChar >= '0' ){ sumNu++; } else if( ' ' == tmpChar ){ } else { sumOther++; } } System.out.println(sumUp+"," + sumLow + "," + sumNu + "," + sumOther); } } String 的方法 byte[] getBytes() 字符串转换为字节数组 char[] toCharArray() 字符串转换为字符数线 String valueOf(char[] data) 字符数组转化为字符串 String valueOf(int i) 把 int 转换为字符串, (valueOf 可以任意类型的数据转化为字符串) String toLowerCase() 字符串转换成小写 String toUpperCase() 字符串转换大写 concat(String str) 字符串拼接 public class Demo10_String { public static void main(String[] args) { String s = "Hello"; byte [] b = s.getBytes(); for(int i=0;i<b.length;i++){ System.out.print(b[i]+" "); } System.out.println(); char[] c = s.toCharArray(); for(int i=0;i<c.length;i++){ System.out.print(c[i] + " "); } System.out.println(); String s2 = String.valueOf(c); System.out.println(s2); String s3 = String.valueOf(10); System.out.println(s3); System.out.println(s.toLowerCase()); System.out.println(s.toUpperCase()); System.out.println(s.concat(" Wrold!")); } } String 练习, 首字母大写, 其他小写 public class Demo11_String { // 字符串首写字母大写, 其他字母小写 public static void main(String[] args) { String s = "what Is A Youth?"; if(s.length() > 0){ System.out.println(s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase()); } } } String 练习, int [] arr = {1,2,3}; 转换成指定格式 {1, 2, 3}的字符串. public class Demo12_String { // int [] arr = {1,2,3}; 转换成指定格式 {1, 2, 3}的字符串. public static void main(String[] args) { int [] arr = {1,2,3,4,5}; String s = ""; if( 1 == arr.length ){ s = "{"+ arr[0]+"}"; } if( arr.length > 1 ){ for (int i=0; i<arr.length; i++) { if ( 0 == i ){ s = "{" + arr[i] + ", "; } else if( i < arr.length - 1 ){ s = s + arr[i] + ", "; } else if( i == arr.length-1 ){ s = s + arr[i] + "}"; } } } System.out.println(s); } } String 中的方法 String replace(char oldChar, char newChar) 用新字符替换所有旧的字符 String replace(CharSequence target, CharSequence replacement) 使用新字符串替换旧的字符串 int compareTo(String anotherString) 比较2个字符串 int compareToIgnoreCase(String str) 忽略大小写比较2个字符串 public class Demo13_String { public static void main(String[] args) { String s = "Switch Mario"; String s2 = s.replace('i','a'); System.out.println(s2); String s3 = s.replace("Mario", "Zelda"); System.out.println(s3); String s4 = " switch one "; System.out.println("{"+s4.trim()+"}"); String s5 = "sEE"; String s6 = "see"; int sum = s5.compareTo(s6); System.out.println(sum); int sum2 = s5.compareToIgnoreCase(s6); System.out.println(sum2); } } String 练习反转字符串 import java.util.Scanner; public class Demo14_String { //反转字符串 public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = ""; String s2 = ""; if( sc.hasNextLine() ){ s = sc.nextLine(); } char [] cs = s.toCharArray(); for (int i=0; i<(cs.length/2); i++) { char temp = cs[cs.length-1-i]; cs[cs.length-1-i] = cs[i]; cs[i] = temp; } System.out.println(new String(cs)); char [] cs2 = s.toCharArray(); for (int i=cs2.length-1; i>=0; i--) { s2 = s2 + cs2[i]; } System.out.println(s2); } } String 练习, 查找str在另一个str出现的次数 public class Demo15_String { public static void main(String[] args) { String sMax = "Whatever is worth doing is worth doing well."; String sMin = "worth"; int sum = 0; int j = 0; int index = 0; while(index<sMax.length()){ j = sMax.indexOf(sMin, index); if(-1 == j){ break; } else{ index = j + 1; sum++; } } System.out.println(sum); } }