Home > Java > javaTutorial > body text

What is the use of startsWith method in java

王林
Release: 2023-05-20 15:07:06
forward
1352 people have browsed it

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&#39;的身份证有:"+number+"个");
	}
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template