【180天】黑马程序员27天视频学习笔记【Day12-下】

叨逼叨两句

  1. 区分哪些能够改变,哪些不能改变,是一等一的智慧。
  2. 带着情绪干事真是不容易,但是规定了一天的课程最多分三次完成,再累再烦也特么要完成!!!

模拟用户登录练习

package test_heima;

import java.util.Scanner;

public class Deom02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i <3; i++) {
            System.out.println("请输入用户名:");
            String userName = sc.nextLine();
            System.out.println("请输入密码:");
            String password = sc.nextLine();
            
            if("admin".equals(userName) && "admin".equals(password)){
                System.out.println("输入正确");
                break;
            } else {
                if(i==2){
                    System.out.println("用户名或密码输入错误,您今天的输入次数用完了");
                    break;
                }
                System.out.println("用户名或密码输入错误,请重新输入,您还有"+(2-i)+"次机会。");
            }
        }
        
    }
}

String类的获取功能

  1. int length():获取字符串的长度
  2. char charAt(int index):获取指定索引位置的字符
  3. int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  4. int indexOf(String str):
  5. int indexOf(int ch,int fromindex):
  6. int indexOf(String str,int fromindex):
  7. int lastindexOf(int ch,int fromindex):
  8. int lastindexOf(String str,int fromindex):
  9. String substring(int start):
  10. String substring(int start,int end):

String类的转换功能【再看看视频12-11】

  1. byte[] getBytes():把字符串转换为字节数组

    1. 默认使用GBK码表
    2. GBK码表包含ASCII码表
    3. GBK码表一个中文代表两个字节
    4. GBK码表中文第一个字节肯定是负数,但是第二个字节未必是。
  2. char[] toCharArray():把字符串转换为字符数组
  3. static String ValueOf(各种类型【包括引用类型】):该方法可将任意类型转换为字符串。
  4. String toLowerCase():字符串转小写
  5. String toUpperCase():字符串转大写
  6. String concat(String str):把字符串拼接

    1. 该方法功能没“+”强大,只能与字符串拼接,而“+”能与任意类型拼接。

String类的替换功能

  1. String replace(char old,char new)
  2. String replace(String old,String new)

String类的去除字符串两端空格

  1. String trim()【用于去除用户习惯性的两端空格】

String的按字典顺序比较两个字符串【看12.14】

  1. int compareTo(String str)
  2. int compareToIgnoreCase(String str)

其它注意事项【看12.14】

  1. char转换字符串使用unicode编码
  2. 字节转换字符串使用GBK编码

评论

暂无评论