java startswith忽略大小写_在Java中使用startsWith和endsWith时,如何忽略大小写?

这是我的代码:

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 (

对于用户如何输入命令,我希望允许他们留有余地,例如:右上,右上,TOPRIGHT,左上等。为此,我试图在最后一点if

(,检查是否cutInput以“ top”或“ up”开头,并检查是否cutInput以“ left”或“

right”结尾,并且不区分大小写。这是可能吗?

这样做的最终目的是允许用户在输入的一行中从三角形的四个方向之一中进行拾取。这是我想到的最好方法,但是对于一般的编程我还是很陌生,可能会使事情变得复杂。如果是的话,这实际上是一种更简单的方法,请告诉我。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#,字符串类型(string)提供了一些成员方法,可以进行忽略大小写的比较,常用的方法有以下几个: 1. Equals方法:可以比较两个字符串是否相等,同指定StringComparison.OrdinalIgnoreCase参数,例如: ``` string str1 = "Hello"; string str2 = "hello"; bool isEqual = str1.Equals(str2, StringComparison.OrdinalIgnoreCase); ``` 2. Compare方法:可以比较两个字符串的大小关系,同指定StringComparison.OrdinalIgnoreCase参数,例如: ``` string str1 = "Hello"; string str2 = "hello"; int compareResult = string.Compare(str1, str2, StringComparison.OrdinalIgnoreCase); // 如果compareResult等于0,则表示两个字符串相等 ``` 3. StartsWith方法和EndsWith方法:可以判断一个字符串是否以另一个字符串开头或结尾,同指定StringComparison.OrdinalIgnoreCase参数,例如: ``` string str = "Hello, world!"; bool startsWith = str.StartsWith("hello", StringComparison.OrdinalIgnoreCase); bool endsWith = str.EndsWith("world", StringComparison.OrdinalIgnoreCase); ``` 4. IndexOf方法和LastIndexOf方法:可以查找一个字符串是否包含另一个字符串,同指定StringComparison.OrdinalIgnoreCase参数,例如: ``` string str = "Hello, world!"; int index = str.IndexOf("world", StringComparison.OrdinalIgnoreCase); int lastIndex = str.LastIndexOf("world", StringComparison.OrdinalIgnoreCase); ``` 这些方法都可以通过指定StringComparison.OrdinalIgnoreCase参数,实现忽略大小写的比较。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值