startswith()方法有兩種變體。本文介紹了所有這些變體,如下所示:1.字符串startsWith():此方法測試字符串是否從第一個索引開始以指定的前綴開頭。
Syntax public boolean startsWith(String prefix) 參數 prefix: the prefix to be matched. Return Value It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.
// Java code to demonstrate the
// working of startsWith()
public class Strt1 {
public static void main(String args[])
{
// Initialising String
String Str = new String("Welcome to geeksforgeeks");
// Testing the prefix using startsWith()
System.out.print("Check whether string starts with Welcome:");
System.out.println(Str.startsWith("Welcome"));
// Testing the prefix using startsWith()
System.out.print("Check whether string starts with geeks:");
System.out.println(Str.startsWith("geeks"));
}
}
輸出:
Check whether string starts with Welcome:true Check whether string starts with geeks:false
2.字符串startsWith(String prefix,int strt_pos):此變體具有兩個參數,並測試字符串是否以指定的前綴開頭並以指定的索引開頭。
Syntax public boolean startsWith(String prefix, int strt_pos) 參數 prefix:the prefix to be matched. strt_pos: where to begin looking in the string. Return Value It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise.
// Java code to demonstrate the
// working of startsWith()
public class Strt2 {
public static void main(String args[])
{
// Initialising String
String Str = new String("Welcome to geeksforgeeks");
// Testing the prefix using startsWith()
System.out.print("Check whether string starts with Welcome at pos 11:");
System.out.println(Str.startsWith("Welcome", 11));
// Testing the prefix using startsWith()
System.out.print("Check whether string starts with geeks at pos 11:");
System.out.println(Str.startsWith("geeks", 11));
}
}
輸出:
Check whether string starts with Welcome at pos 11:false Check whether string starts with geeks at pos 11:true
可能的應用:此方法主要可用於過濾前綴,例如。過濾以數字開頭的電話號碼或以特定字母開頭的姓名。本文對此進行了解釋。
// Java code to demonstrate the
// application of startsWith()
public class Appli {
public static void main(String args[])
{
// Initialising String
String Str = new String("Astha Tyagi");
// Testing the prefix using startsWith()
System.out.print("Check whether Astha Tyagi starts with A:");
System.out.println(Str.startsWith("A"));
}
}
輸出:
Check whether Astha Tyagi starts with A:true
相關用法
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
注:本文由純淨天空篩選整理自 Java.lang.String.startswith() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。