java startswith忽略大小写_Java String startsWith()方法与示例
字符串startsWith()方法
startsWith()方法是一個String類方法,用于檢查給定的字符串是否以特定的字符序列開頭。
如果字符串以給定的字符序列開頭-startsWith()方法將返回true,如果字符串不是以給定的字符序列開頭-則startsWith()方法將返回false。
語法:boolean?String_object.startsWith(character_sequence);
這里,String_object是主要字符串,我們必須在其中檢查是否以給定的character_sequence開頭。
character_sequence是要檢查的字符集。
示例Input:
str?=?"Hello?world!"
Function?call:
str.startsWith("Hello");
Output:
true
Input:
str?=?"NHOOO"
Function?call:
str.startsWith("inc");
Output:
false
碼:public?class?Main
{
public?static?void?main(String[]?args)?{
String?str1?=?"Hello?world!";
String?str2?=?"NHOOO";
System.out.println(str1.startsWith("Hello"));
System.out.println(str1.startsWith("inc"));
//檢查條件
if(str1.startsWith("Hello")){
System.out.println(str1?+?"?starts?with?Hello"?);
}
else{
System.out.println(str1?+?"?does?not?start?with?Hello"?);
}
//注意:方法區分大小寫
if(str2.startsWith("inc")){
System.out.println(str2?+?"?starts?with?inc"?);
}
else{
System.out.println(str2?+?"?does?not?start?with?inc"?);
}
}
}
輸出結果true
false
Hello?world!?starts?with?Hello
NHOOO?does?not?start?with?inc
總結
以上是生活随笔為你收集整理的java startswith忽略大小写_Java String startsWith()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中if条件结构_Java的控制结
- 下一篇: java怎么做沙子合并_dp之沙子合并