Three methods for searching Java strings and three methods for searching java strings

Source: Internet
Author: User

Three methods for searching Java strings and three methods for searching java strings

Indexof method:

Note: The indexOf method returns an integer indicating the starting position of the substring in the String object. If no substring is found,-1 is returned.

Public class IndexOf {public static void main (String [] args) {String s = "Li Hong # Wang Hai # Lin Qiao # LU Xun # Tang Mei"; String q = "#"; // String err = "*"; // The int I = 0; for (int j = 0; j <s. length (); j ++) {// loop all strings String get = s. substring (j, j + 1); // print all strings if (get. equals (q) {// judge # whether the word shows I ++; // # The number of times the word appears} System. out. println ("Total" + s. length () + "string"); System. out. println ("# A total of" + I + "times"); // # The total number of times the characters appear. System. out. println ("the first # word appears in the string" + s. indexOf (q) + "location"); if (s. indexOf (err) =-1) {// If-1 is returned, the System does not exist in the character string. out. println ("* the word does not exist in the string ");}}}

Running result:

A total of 14 strings
# The word appears four times in total
First # The word appears at two positions of the string
* The word does not exist in the string.

StartsWith method:

Note: The startsWith () method is used to check whether a string starts with a specified prefix.

Public class StartWith {public static void main (String [] args) {String id [] = {"53011198902280308", "52011198711038269", "53011197701328291"}; int number = 0; system. out. println ("matching strings"); for (int I = 0; I <id. length; I ++) {if (id [I]. startsWith ("530") = true) {number ++; System. out. println (id [I]) ;}} System. out. println ("the ID cards with the first three characters '000000' include:" + number + ");}}

Running result:

The matching strings are
53011198902280308
53011197701328291
There are two ID cards with the first three characters of '123'

RegionMatches method:

Note: The regionMatches () method is used to check whether two strings are equal in one region.

Public class RegionMatches {public static void main (String [] args) {int number = 0; String s = "student; entropy; ENgage, English, client, eye "; string q = "en"; // the String to be searched for (int k = 0; k <s. length (); k ++) {// true is case-insensitive, k is all strings, q is the string to be searched, 0 is from the position of string 1, 2. The length of the string to be searched is 2if (s. regionMatches (true, k, q, 0, 2) {number ++; System. out. println ("en character in the character string" + k + "position") ;}} System. out. println ("Total number of strings containing 'en' substrings:" + number );}}

Running result:

The en character is at the 4th position of the string.
The en character is at the 8th position of the string.
The en character is at the 16th position of the string.
The en character is at the 23rd position of the string.
The en character is at the 34th position of the string.
The total number of strings containing 'en' substrings is: 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.