我需要获取字符串中的子字符串位置,而忽略子字符串区分大小写。这意味着我希望得到相同的输出结果的情况下:获取子字符串位置忽略区分大小写
String p = "aaaHelloddd";
System.out.println(p.indexOf("Hello"));
System.out.println(p.indexOf("hello"));
如何实现这一目标?
我需要获取字符串中的子字符串位置,而忽略子字符串区分大小写。这意味着我希望得到相同的输出结果的情况下:获取子字符串位置忽略区分大小写
String p = "aaaHelloddd";
System.out.println(p.indexOf("Hello"));
System.out.println(p.indexOf("hello"));
如何实现这一目标?
您只需更改为大写并检查大写字符串。
String p = "aaaHelloddd";
System.out.println(p.toUpperCase().indexOf("HELLO"));
如果Hello是一个参数,只需使其大写。
String p = "aaaHelloddd";
System.out.println(p.toUpperCase().indexOf("hello".toUpperCase()));
System.out.println(p.toUpperCase().indexOf("Hello".toUpperCase()));
两者都打印相同的结果。
使用toLowerCase()
System.out.println(p.toLowerCase().indexOf("Hello".toLowerCase()));
System.out.println(p.toLowerCase().indexOf("hello".toLowerCase()));
你可能想小写你的“你好”然后;) –
我给你一个加号。你的回答是正确的! –
我不明白下标记。你可以解释吗? –
我没有downvote,但它可能与您回答重复问题的事实有关。有些人认为,作为代表农业和降低这些类型问题的所有答案。 – Tunaki
如果已经回答了,则downvote不正确的答案也是正确的! –