Java 11-8 finding small strings in a large string case

Source: Internet
Author: User


1. Count the number of occurrences of a large string of small strings
Example:
In the string "Woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"
Results:
Java has appeared 5 times
Analysis:
1, first already know the string
A: Define a statistic variable = 0;
B: To find the existence of a small string in a large string, use int indexOf (string str): Returns the index of the first occurrence of the specified character in this string.
A: If the returned index value is-1, the string does not exist in the large string, and the output statistic variable
B: If the return is not 1, it is the index of the first character of the string in the large string, and this time the statistic variable + +
C: Starting with the resulting index, plus the length of the string, to the end of the strings, start to intercept a new string, and then assign the string to a large string, replacing the previous large string
string substring (int start): Intercepts the string starting at the specified position, at the end of the default.
D: Start looping again from B until the new string is given without the small string, that is, a in B
The above analysis is defined as a method with two elements of the method:
A: return value: int
B: List of parameters: two strings, large strings and small strings

1  Public classStringTest3 {2 3  Public Static voidMain (string[] args) {4 //define known large strings and small strings5String maxstring = "Woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";6String minstring = "java";7 8 //calling the GetCount method9 intCount =GetCount (maxstring,minstring);TenSYSTEM.OUT.PRINTLN ("Java has appeared in this large string of" +count+ "Times"); One } A  - //Define this method -  Public Static intGetCount (String max,string min) { the //Define a statistic variable - intCount = 0 ; -  - //finds whether a small string exists in a large string, with int indexOf (string str): Returns the index of the first occurrence of the specified character in this string.  + intindex =max.indexof (min); -  + //the judgment of the index A  while(Index! =-1){ at //statistical variable self-increment -Count + +; -  - //Starting with the resulting index, plus the length of the string, to the end of the strings, start to intercept a new string, and then assign the string to a large string, replacing the previous large string - //string substring (int start): Intercepts the string starting at the specified position, at the end of the default.  -  inmax = Max.substring (index+min.length ());  -  toindex = max.indexof (min);//Find out if a small string exists in a large string again + } - returncount; the  * } $ Panax Notoginseng }


2, optimized version of the number of small and medium-sized string occurrences
Requirements: First enter a large string, and then enter a small string, query small string in the large string appeared several times
Analysis:
1, first keyboard input 2 strings
A: Define a statistic variable = 0;
B: To find the existence of a small string in a large string, use int indexOf (string str): Returns the index of the first occurrence of the specified character in this string.
A: If the returned index value is-1, the string does not exist in the large string, and the output statistic variable
B: If the return is not 1, it is the index of the first character of the string in the large string, and this time the statistic variable + +
C: Starting with the resulting index, plus the length of the string, to the end of the strings, start to intercept a new string, and then assign the string to a large string, replacing the previous large string
string substring (int start): Intercepts the string starting at the specified position, at the end of the default.
D: Start looping again from B until the new string is given without the small string, that is, a in B
The above analysis is defined as a method with two elements of the method:
A: return value: int
B: List of parameters: two strings, large strings and small strings


1 ImportJava.util.Scanner;2  Public classSTRINGTEXT4 {3 4  Public Static voidMain (string[] args) {5 //1, first keyboard input 2 strings6Scanner sc =NewScanner (system.in);7System.out.println ("Please enter a large string:");8String maxstring =sc.nextline ();9System.out.println ("Please enter a small string:");TenString minstring =sc.nextline (); One  A //calling the GetCount method - intCount =GetCount (maxstring,minstring); -System.out.println (minstring+ "+count+" appears in "+maxstring+"); the  - } - //Defining Methods -  Public Static intGetCount (String max,string min) { + //Define a statistic variable - intCount = 0; + //finds whether a small string exists in a large string, with int indexOf (string str): Returns the index of the first occurrence of the specified character in this string.  A intindex; at  while((Index=max.indexof (min))! =-1){ - //when the resulting index is not-1 o'clock -count++; - //Starting with the resulting index, plus the length of the string, to the end of the strings, start to intercept a new string, and then assign the string to a large string, replacing the previous large string -max = max.substring (max.indexof (min) +min.length ()); - } in returncount; - } to  +}

Java 11-8 finding small strings in a large string case

Related Article

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.