赞
踩
API&字符串
API
字符串
创建string对象
字符串的内存管理
串池:字符串常量池,只有直接赋值方式的字符串才存到这里,new出来的字符串不在
复用相同的地址(节约内存)
有new就是在堆内存中开辟了新的空间,使用不同的地址(可能会浪费内存)
String比较
==?比较
注意有没有new,存的可能是地址
直接赋值字符串(那就是在串池里),如果内容相同,则会复用一样的地址
所以==号出来,结果是true
Equals
键盘录入的字符串是new出来的(源码写的)
Ctrl+alt+t:快捷键包裹一段代码,并用if、for、while包裹
- package exercise;
-
- import java.util.Scanner;
-
- public class exercise2 {
- public static void main(String[] args) {
- String rightUsername = "zhangsan";
- String rightPassword = "123456";
-
- Scanner sc = new Scanner(System.in);
-
- int chance = 3;
- for (int i = 0; i < chance; i++) {
-
- System.out.println("请输入用户名:");
- String username = sc.next();
- System.out.println("请输入密码:");
- String password = sc.next();
-
- boolean flag1 = username.equals(rightUsername);
- boolean flag2 = password.equals(rightPassword);
-
- if (flag1 && flag2) {
- System.out.println("登录成功!");
- break;
- } else {
- if (i == chance - 1) {
- System.out.println("账号"+username + "将被锁定!");
- }else {
- System.out.println("登录失败,用户名或密码错误!还剩下" + (chance - i - 1) + "次机会");
- }
- }
- }
-
- }
- }
遍历字符串 charAt
统计大小写和数字
记得char字符的比较是转化为ASCII码来进行比较的
拼接字符串
- package exercise;
-
- public class exercise4 {
- public static void main(String[] args) {
- int[] arr = {1, 2, 3, 99999, 23};
- String str = arrToString(arr);
- System.out.println(str);
- }
-
- public static String arrToString(int[] arr) {
- if (arr == null) {
- return "";
- }
-
- if (arr.length == 0) {
- return "[]";
- }
-
- String str = "[";
- for (int i = 0; i < arr.length; i++) {
- if (i == arr.length - 1) {
- str = str + arr[i] + "]";
- } else {
- str = str + arr[i] + ", ";
- }
- }
-
- return str;
-
- }
- }
字符串反转
Shift + f6: 批量修改,像是for里面的形参
- package exercise;
-
- import java.util.Scanner;
-
- public class exercise5 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入一个字符串:");
- String str = sc.next();
- String reverseStr = reverse(str);
- System.out.println(reverseStr);
- }
-
- public static String reverse(String str) {
- String result = "";
- for (int i = str.length() - 1; i >= 0; i--) {
- char c = str.charAt(i);
- result = result + c;
- }
- return result;
- }
- }
查表法
- package exercise;
-
- import java.util.Scanner;
-
- public class exercise6 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int money;
- while (true) {
- System.out.println("输入金额:");
- money = sc.nextInt();
- if (money >= 0 && money <= 9999999) {
- break;
- } else {
- System.out.println("无效金额!");
- }
- }
-
- String moneyStr = "";
- while (true) {
- int ge = money % 10;
- String CapitalNumber = getCapitalNumber(ge);
- moneyStr = CapitalNumber + moneyStr;
-
- money /= 10;
- if (money == 0) {
- break;
- }
-
- }
-
- int count = 7 - moneyStr.length();
- for (int i = 0; i < count; i++) {
- moneyStr = "零" + moneyStr;
- }
-
- String[] arr = {"佰","拾","万","仟","佰","拾","元"};
- String result = "";
- for (int i = 0; i < moneyStr.length(); i++) {
- char c = moneyStr.charAt(i);
- result = result + c + arr[i];
- }
-
- System.out.println(result);
- }
-
- public static String getCapitalNumber(int number) {
- String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
- return arr[number];
- }
- }
截取字符串 substring
- package exercise;
-
- public class exercise7 {
- public static void main(String[] args) {
- String phoneNumber = "12345854125";
- String start = phoneNumber.substring(0,3);
- String end = phoneNumber.substring(7);
- String result = start + "****" + end ;
- System.out.println(result);
- }
- }
- package exercise;
-
- public class exercise8 {
- public static void main(String[] args) {
- String id = "354862495157865234";
- String year = id.substring(6, 10);
- String month = id.substring(10, 12);
- String day = id.substring(12, 14);
- char gender = id.charAt(16);
- int num = gender - 48;
-
- String birth = "出生年月日:" + year + "年" + month + "月" + day + "日";
- String genderInfo = "";
- if (num % 2 == 1) {
- genderInfo = "男";
- }else {
- genderInfo = "女";
- }
-
- System.out.println(birth);
- System.out.println(genderInfo);
-
- }
- }
字符串替换 replace
Stringbuilder 在字符串拼接,反转用到
用+号拼接字符串很慢,因为每次拼接都会在内存中产生一个新的字符串,占据内存
Append reverse tostring
直接打印空的stringbuilder ,是空值,而不是地址值
append 、reverse是在容器内部进行处理,是可以改变的,不需要新建一个变量接受结果,和字符串不一样。
- package exercise;
-
- import java.util.Scanner;
-
- public class exercise9 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("input string:");
- String str = sc.next();
-
- String reverseStr = new StringBuilder(str).reverse().toString();
- if (str.equals(reverseStr)){
- System.out.println("是对称的!");
- }else {
- System.out.println("不是对称的!");
- }
-
- }
- }
- package exercise;
-
- public class exercise10 {
- public static void main(String[] args) {
- int[] arr = {1, 2, 3, 1518, 1115};
- System.out.println(arrToString(arr));
- }
-
- public static String arrToString(int[] arr) {
- StringBuilder sb = new StringBuilder();
-
- sb.append("[");
- String result = "";
- for (int i = 0; i < arr.length; i++) {
- if (i == arr.length - 1) {
- sb.append(arr[i]);
- } else {
- sb.append(arr[i]).append(", ");
- }
- }
- sb.append("]");
-
- return sb.toString();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。