当前位置:   article > 正文

Java学习笔记(8)

Java学习笔记(8)

API&字符串

API

字符串

创建string对象

字符串的内存管理

串池:字符串常量池,只有直接赋值方式的字符串才存到这里,new出来的字符串不在

复用相同的地址(节约内存)

有new就是在堆内存中开辟了新的空间,使用不同的地址(可能会浪费内存)

String比较

==?比较

注意有没有new,存的可能是地址

直接赋值字符串(那就是在串池里),如果内容相同,则会复用一样的地址

所以==号出来,结果是true

Equals

键盘录入的字符串是new出来的(源码写的)

Ctrl+alt+t:快捷键包裹一段代码,并用if、for、while包裹

  1. package exercise;
  2. import java.util.Scanner;
  3. public class exercise2 {
  4. public static void main(String[] args) {
  5. String rightUsername = "zhangsan";
  6. String rightPassword = "123456";
  7. Scanner sc = new Scanner(System.in);
  8. int chance = 3;
  9. for (int i = 0; i < chance; i++) {
  10. System.out.println("请输入用户名:");
  11. String username = sc.next();
  12. System.out.println("请输入密码:");
  13. String password = sc.next();
  14. boolean flag1 = username.equals(rightUsername);
  15. boolean flag2 = password.equals(rightPassword);
  16. if (flag1 && flag2) {
  17. System.out.println("登录成功!");
  18. break;
  19. } else {
  20. if (i == chance - 1) {
  21. System.out.println("账号"+username + "将被锁定!");
  22. }else {
  23. System.out.println("登录失败,用户名或密码错误!还剩下" + (chance - i - 1) + "次机会");
  24. }
  25. }
  26. }
  27. }
  28. }

遍历字符串 charAt

统计大小写和数字

记得char字符的比较是转化为ASCII码来进行比较的

拼接字符串

  1. package exercise;
  2. public class exercise4 {
  3. public static void main(String[] args) {
  4. int[] arr = {1, 2, 3, 99999, 23};
  5. String str = arrToString(arr);
  6. System.out.println(str);
  7. }
  8. public static String arrToString(int[] arr) {
  9. if (arr == null) {
  10. return "";
  11. }
  12. if (arr.length == 0) {
  13. return "[]";
  14. }
  15. String str = "[";
  16. for (int i = 0; i < arr.length; i++) {
  17. if (i == arr.length - 1) {
  18. str = str + arr[i] + "]";
  19. } else {
  20. str = str + arr[i] + ", ";
  21. }
  22. }
  23. return str;
  24. }
  25. }

字符串反转

Shift + f6: 批量修改,像是for里面的形参

  1. package exercise;
  2. import java.util.Scanner;
  3. public class exercise5 {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. System.out.println("请输入一个字符串:");
  7. String str = sc.next();
  8. String reverseStr = reverse(str);
  9. System.out.println(reverseStr);
  10. }
  11. public static String reverse(String str) {
  12. String result = "";
  13. for (int i = str.length() - 1; i >= 0; i--) {
  14. char c = str.charAt(i);
  15. result = result + c;
  16. }
  17. return result;
  18. }
  19. }

查表法

  1. package exercise;
  2. import java.util.Scanner;
  3. public class exercise6 {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int money;
  7. while (true) {
  8. System.out.println("输入金额:");
  9. money = sc.nextInt();
  10. if (money >= 0 && money <= 9999999) {
  11. break;
  12. } else {
  13. System.out.println("无效金额!");
  14. }
  15. }
  16. String moneyStr = "";
  17. while (true) {
  18. int ge = money % 10;
  19. String CapitalNumber = getCapitalNumber(ge);
  20. moneyStr = CapitalNumber + moneyStr;
  21. money /= 10;
  22. if (money == 0) {
  23. break;
  24. }
  25. }
  26. int count = 7 - moneyStr.length();
  27. for (int i = 0; i < count; i++) {
  28. moneyStr = "零" + moneyStr;
  29. }
  30. String[] arr = {"佰","拾","万","仟","佰","拾","元"};
  31. String result = "";
  32. for (int i = 0; i < moneyStr.length(); i++) {
  33. char c = moneyStr.charAt(i);
  34. result = result + c + arr[i];
  35. }
  36. System.out.println(result);
  37. }
  38. public static String getCapitalNumber(int number) {
  39. String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
  40. return arr[number];
  41. }
  42. }

截取字符串 substring

  1. package exercise;
  2. public class exercise7 {
  3. public static void main(String[] args) {
  4. String phoneNumber = "12345854125";
  5. String start = phoneNumber.substring(0,3);
  6. String end = phoneNumber.substring(7);
  7. String result = start + "****" + end ;
  8. System.out.println(result);
  9. }
  10. }

  1. package exercise;
  2. public class exercise8 {
  3. public static void main(String[] args) {
  4. String id = "354862495157865234";
  5. String year = id.substring(6, 10);
  6. String month = id.substring(10, 12);
  7. String day = id.substring(12, 14);
  8. char gender = id.charAt(16);
  9. int num = gender - 48;
  10. String birth = "出生年月日:" + year + "年" + month + "月" + day + "日";
  11. String genderInfo = "";
  12. if (num % 2 == 1) {
  13. genderInfo = "男";
  14. }else {
  15. genderInfo = "女";
  16. }
  17. System.out.println(birth);
  18. System.out.println(genderInfo);
  19. }
  20. }

字符串替换 replace

Stringbuilder 在字符串拼接,反转用到

用+号拼接字符串很慢,因为每次拼接都会在内存中产生一个新的字符串,占据内存

Append reverse tostring

直接打印空的stringbuilder ,是空值,而不是地址值

append 、reverse是在容器内部进行处理,是可以改变的,不需要新建一个变量接受结果,和字符串不一样。

  1. package exercise;
  2. import java.util.Scanner;
  3. public class exercise9 {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. System.out.println("input string:");
  7. String str = sc.next();
  8. String reverseStr = new StringBuilder(str).reverse().toString();
  9. if (str.equals(reverseStr)){
  10. System.out.println("是对称的!");
  11. }else {
  12. System.out.println("不是对称的!");
  13. }
  14. }
  15. }

  1. package exercise;
  2. public class exercise10 {
  3. public static void main(String[] args) {
  4. int[] arr = {1, 2, 3, 1518, 1115};
  5. System.out.println(arrToString(arr));
  6. }
  7. public static String arrToString(int[] arr) {
  8. StringBuilder sb = new StringBuilder();
  9. sb.append("[");
  10. String result = "";
  11. for (int i = 0; i < arr.length; i++) {
  12. if (i == arr.length - 1) {
  13. sb.append(arr[i]);
  14. } else {
  15. sb.append(arr[i]).append(", ");
  16. }
  17. }
  18. sb.append("]");
  19. return sb.toString();
  20. }
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/219684
推荐阅读
相关标签
  

闽ICP备14008679号