在Java中使用startsWith和endsWith时,如何忽略大小写?
这是我的密码:在Java中使用startsWith和endsWith时,如何忽略大小写?,java,string,case-insensitive,startswith,ends-with,Java,String,Case Insensitive,Startswith,Ends With,这是我的密码: public static void rightSel(Scanner scanner,char t) { /*if (!stopping)*/System.out.print(": "); if (scanner.hasNextLine()) { String orInput = scanner.nextLine; if (orInput.equalsIgnoreCase("help") {
public static void rightSel(Scanner scanner,char t)
{
/*if (!stopping)*/System.out.print(": ");
if (scanner.hasNextLine())
{
String orInput = scanner.nextLine;
if (orInput.equalsIgnoreCase("help")
{
System.out.println("The following commands are available:");
System.out.println(" 'help' : displays this menu");
System.out.println(" 'stop' : stops the program");
System.out.println(" 'topleft' : makes right triangle alligned left and to the top");
System.out.println(" 'topright' : makes right triangle alligned right and to the top");
System.out.println(" 'botright' : makes right triangle alligned right and to the bottom");
System.out.println(" 'botleft' : makes right triangle alligned left and to the bottom");
System.out.println("To continue, enter one of the above commands.");
}//help menu
else if (orInput.equalsIgnoreCase("stop")
{
System.out.println("Stopping the program...");
stopping = true;
}//stop command
else
{
String rawInput = orInput;
String cutInput = rawInput.trim();
if (
我想让用户在如何输入命令方面有一些回旋余地,比如:右上角、右上角、右上角、左上角等等。
为此,我试图在最后一个if(
),检查cutInput
是否以“top”或“up”开头,检查cutInput
是否以“left”或“right”结尾,同时不区分大小写。这可能吗
这样做的最终目标是允许用户在一行输入中,从三角形的四个方向中选择一个。这是我能想到的最好的方法,但我对一般编程还是很陌生,可能会使事情过于复杂。如果是的话,有一种更简单的方法,请让我知道。像这样:
aString.toUpperCase().startsWith("SOMETHING");
aString.toUpperCase().endsWith("SOMETHING");
接受的答案是错误的。如果查看
String.equalsIgnoreCase()
的实现,您会发现需要比较字符串的小写和大写版本,然后才能最终返回false
以下是我自己的版本,基于:
我在书中做了一个练习,练习内容是:“制作一个方法,测试字符串的结尾是否以‘ger’结尾。将代码写入其中,测试短语‘ger’中大写字母和小写字母的任意组合。” 所以,基本上,它要求我测试字符串中的一个短语,忽略大小写,所以“ger”中的字母是大写还是小写都无关紧要。下面是我的解决方案:
package exercises;
import javax.swing.JOptionPane;
public class exercises
{
public static void main(String[] args)
{
String input, message = "enter a string. It will"
+ " be tested to see if it "
+ "ends with 'ger' at the end.";
input = JOptionPane.showInputDialog(message);
boolean yesNo = ends(input);
if(yesNo)
JOptionPane.showMessageDialog(null, "yes, \"ger\" is there");
else
JOptionPane.showMessageDialog(null, "\"ger\" is not there");
}
public static boolean ends(String str)
{
String input = str.toLowerCase();
if(input.endsWith("ger"))
return true;
else
return false;
}
}
正如您从代码中看到的,我只是将用户输入的字符串转换为所有小写。如果每个字母都在小写和大写之间交替,这并不重要,因为我否定了这一点。使用
command.toLowerCase().StartWith(“up”)
和command.toLowerCase().endsWith(“右”)
。.toUpperCase()
即使字符串包含非字母字符也可以工作吗?另外,您知道这些字符是否可以使用逻辑(al?)运算符组合成条件吗?@MataoGearsky当然可以。非字母字符将被toUpperCase()忽略
。毫无疑问,上述内容可以与逻辑运算符结合使用(为什么不可以?它们只是布尔表达式。)这个答案是错误的。如果您查看String.equalsIgnoreCase()的实现
您会发现,在最终返回false
之前,您需要比较字符串的小写和大写版本。请参阅我的答案。其性能不是最佳的。如果您的字符串很大怎么办?这仅仅是因为格鲁吉亚字母吗?@bphilipnyc正确。这是一个例子受字符串源代码的影响,但我们不知道它是否是唯一的一个。安全总比抱歉好。此外,此方法避免了对字符串中永远不会检查的部分进行小写和复制。旁注:Spring框架也与Gili的答案一致:这是一个具体的示例,说明了为什么我们需要同时比较小写和小写大写版本:
package exercises;
import javax.swing.JOptionPane;
public class exercises
{
public static void main(String[] args)
{
String input, message = "enter a string. It will"
+ " be tested to see if it "
+ "ends with 'ger' at the end.";
input = JOptionPane.showInputDialog(message);
boolean yesNo = ends(input);
if(yesNo)
JOptionPane.showMessageDialog(null, "yes, \"ger\" is there");
else
JOptionPane.showMessageDialog(null, "\"ger\" is not there");
}
public static boolean ends(String str)
{
String input = str.toLowerCase();
if(input.endsWith("ger"))
return true;
else
return false;
}