Home >Java >javaTutorial >What is the use of startsWith method in java
startsWith method:
Note: startsWith() method is used to detect whether the string starts with the specified prefix.
public class StartWith{ public static void main(String[] args){ String id[]= {"53011198902280308","52011198711038269","53011197701328291"}; int number = 0; System.out.println("符合条件的字符串有"); for(int i=0;i<id.length;i++) { if(id[i].startsWith("530") == true) { number++; System.out.println(id[i]); } } System.out.println("前面3个字符为‘530'的身份证有:"+number+"个"); } }
Running result:
The strings that meet the conditions are
53011198902280308
53011197701328291
The ID cards whose first three characters are '530' are: 2 indivual
The above is the detailed content of What is the use of startsWith method in java. For more information, please follow other related articles on the PHP Chinese website!