I used to do the use for loop traversal, but today suddenly someone asked in the simplest way to achieve the effect, the infiltration died a bit. Suddenly feel that the previous thought too low, many problems are not thought to strive to do the best.
The simplest code ideas are now shared as follows:
In java.lang.string, there is a common string splitting method String.Split ("str"). Using this method, we can split the string into a string array as we requested. Then use. length to get the length of the string array int, with int-1, you get the number of times that STR appears in string! Isn't it easy? In the programming world, thought is really the first.
The code is as follows:
Count the number of "7*r" contained in a string
public class Twst002 {
public static void Main (string[] args) {
String ss= "Weexc7*reptiondfexc7*reptionfh7*rgexcepti77*rondryexc77*reption";
String[] s = ss.split ("7*r");
System.out.println ("Number of 7*r:" + (s.length-1));
}
}
The result of the operation is: 7*r number: 5
The quickest way to find the number of a string in a string