忽略Java中的大写和小写 [英] Ignoring upper case and lower case in Java

查看:37
本文介绍了忽略Java中的大写和小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用户输入的任何内容在我的方法中忽略大小写:

I want to know how to make whatever the user inputs to ignore case in my method:

public static void findPatient() {
    if (myPatientList.getNumPatients() == 0) {
        System.out.println("No patient information is stored.");
    }
    else {
        System.out.print("Enter part of the patient name: ");
        String name = sc.next();
        sc.nextLine();
        System.out.print(myPatientList.showPatients(name));
    }
}

推荐答案

您必须同时使用 String 方法 .toLowerCase().toUpperCase()输入和您尝试与之匹配的字符串.

You have to use the String method .toLowerCase() or .toUpperCase() on both the input and the string you are trying to match it with.

示例:

public static void findPatient() {
    System.out.print("Enter part of the patient name: ");
    String name = sc.nextLine();

    System.out.print(myPatientList.showPatients(name));
}

//the other class
ArrayList<String> patientList;

public void showPatients(String name) {
    boolean match = false;

    for(String matchingname : patientList) {
        if (matchingname.toLowerCase().contains(name.toLowerCase())) {
            match = true;
        }
    }
}

这篇关于忽略Java中的大写和小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆