The quickest way to find the number of a string in a string

Source: Internet
Author: User

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

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.