首页 > 解决方案 > 字符串的额外 java 输入验证

问题描述

我想这样做以便仍然可以检测到短输入,例如“Londo”和“Lon”,但希望保持较小并使用它而无需基本上复制和粘贴代码,有什么提示吗?谢谢你。

if (Menu.answer1.equals("London"))
        {
         if (location.equals("London")) {
             System.out.print(location + " ");
             System.out.print(date + " ");
             System.out.print(degrees + "C ");
             System.out.print(wind + "MPH ");
             System.out.print(winddirection + " ");
             System.out.print(weather + " ");
             System.out.println("");
         }

标签: javastring

解决方案


您可以使用startsWith()

String city = "London";
if (city.startsWith("Lon")) {
    // do something
}

推荐阅读