GVKun编程网logo

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

8

此处将为大家介绍关于在Java中使用startsWith和endsWith时,如何忽略大小写?的详细内容,并且为您解答有关java忽略大小写的相关问题,此外,我们还将为您介绍关于c#–比较字符串时如何

此处将为大家介绍关于在Java中使用startsWith和endsWith时,如何忽略大小写?的详细内容,并且为您解答有关java 忽略大小写的相关问题,此外,我们还将为您介绍关于c# – 比较字符串时如何忽略大小写?、es6 includes(), startsWith(), endsWith()、es6 includes(),startsWith(),endsWith()、java – 为什么String.endsWith和String.startWith不一致?的有用信息。

本文目录一览:

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

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

这是我的代码:

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”结尾,并且不区分大小写。这是可能吗?

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

答案1

小编典典

像这样:

aString.toUpperCase().startsWith("SOMETHING");aString.toUpperCase().endsWith("SOMETHING");

c# – 比较字符串时如何忽略大小写?

c# – 比较字符串时如何忽略大小写?

我正在使用 linq搜索列表(用户在文本框中输入查询).

我想要这样做是不区分大小写,并尝试使用IgnoreCase,但我不知道在哪里放它….我知道我可以使用上或下,但我想听到有人有任何替代方法吗?什么是最佳实践?
正则表达式似乎还没有工作?

string searchQuery = tbSearchQuery.Text;
        var something= from x in y 
                       where x.Subject.Contains(searchQuery)
                       select x;

解决方法

使用 string.Compare:
var something= from x in y 
                   where string.Compare(x.Subject,searchQuery,true) >= 0
                   select x;

这也可以处理任何字符串为空的情况.

es6 includes(), startsWith(), endsWith()

es6 includes(), startsWith(), endsWith()

传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中。ES6 又提供了三种新方法。

  • includes():返回布尔值,表示是否找到了参数字符串。
  • startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。
  • endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。
let s = ''Hello world!'';

s.startsWith(''Hello'') // true
s.endsWith(''!'') // true
s.includes(''o'') // true

这3个方法的第二个参数表示搜索的起始位置,默认为0。如果第二个参数为负数,则表示倒数的位置,如果这时它大于数组长度(比如第二个参数为-4,但数组长度为3),则会重置为从0开始。

es6 includes(),startsWith(),endsWith()

es6 includes(),startsWith(),endsWith()

includes(), startsWith(), endsWith()

传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中。

ES6又提供了三种新方法。

includes():返回布尔值,表示是否找到了参数字符串。

startsWith():返回布尔值,表示参数字符串是否在源字符串的头部。

endsWith():返回布尔值,表示参数字符串是否在源字符串的尾部。

1 var s = 'Hello World!';
2 
3 s.startsWith('Hello') // true
4 s.endsWith('!') // true
5 s.includes('o') // true

这三个方法都支持第二个参数,表示开始搜索的位置。

1 var s = 'Hello World!';
2 
3 s.startsWith('world', 6) // true
4 s.endsWith('Hello', 5) // true
5 s.includes('Hello', 6) // false

上面代码表示,使用第二个参数n时,endsWith的行为与其他两个方法有所不同。它针对前n个字符,而其他两个方法针对从第n个位置直到字符串结束。

java – 为什么String.endsWith和String.startWith不一致?

java – 为什么String.endsWith和String.startWith不一致?

参见英文答案 > lower case of turkish character dotted i                                    1个
我有以下测试用例,只有第一个断言通过.为什么?

@Test
public void test() {
    String i1 = "i";
    String i2 = "İ".toLowerCase();

    System.out.println((int)i1.charat(0)); // 105
    System.out.println((int)i2.charat(0)); // 105

    assertTrue(i2.startsWith(i1));

    assertTrue(i2.endsWith(i1));
    assertTrue(i1.endsWith(i2));
    assertTrue(i1.startsWith(i2));
}

回复后更新

我想要的是使用startsWith和endsWith以不区分大小写的方式,使得下面的表达式应该返回true.

"ALİ".toLowerCase().endsWith("i");

我想这与C#和Java不同.

解决方法

这是因为小写İ(“拉丁大写字母
我在英文语言环境中使用上面的点数转换成两个字符:“拉丁小写字母i”和“组合点上方”.

这解释了为什么它从i开始,但不以i结束(它以结合变音符号结束).

在土耳其语语言环境中,小写字母İ根据土耳其语言学规则简单地变成“拉丁语小写字母i”,因此您的代码将起作用.

这是一个测试程序,以帮助弄清楚发生了什么:

class Test {
  public static void main(String[] args) {
    char[] foo = args[0].toLowerCase().tochararray();
    System.out.print("Lowercase " + args[0] + " has " + foo.length + " chars: ");
    for(int i=0; i<foo.length; i++) System.out.print("0x" + Integer.toString((int)foo[i],16) + " ");
    System.out.println();
  }
}

这是我们在配置为英语的系统上运行它时得到的结果:

$LC_ALL=en_US.utf8 java Test "İ"
Lowercase İ has 2 chars: 0x69 0x307

以下是我们在为土耳其语配置的系统上运行时获得的内容:

$LC_ALL=tr_TR.utf8 java Test "İ"
Lowercase İ has 1 chars: 0x69

这甚至是07 doc的api文档使用的特定示例,这是您可以用来获取特定区域设置中的小写版本而不是系统默认区域设置的方法.

关于在Java中使用startsWith和endsWith时,如何忽略大小写?java 忽略大小写的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于c# – 比较字符串时如何忽略大小写?、es6 includes(), startsWith(), endsWith()、es6 includes(),startsWith(),endsWith()、java – 为什么String.endsWith和String.startWith不一致?的相关信息,请在本站寻找。

本文标签: