The Java implementation iterates through each letter of a string (summary)

Source: Internet
Author: User

Basics: Keep in mind the various methods of string manipulation:

? ??

?        String s = "AAALJLFEAKDSFLKJSADJAEFDSAFHAASDASD";                Number of occurrences        int num = 0;                 Loop through each character to determine if it is a character a, if yes, the number of increments for        (       //Input code                   )        {            //To get each character, to determine whether the character a            if (     //Input code            ) {                //Cumulative statistics Count                num++;             }        }        System.out.println ("The number of occurrences of character a:" + num);?

1.

? int num = 0;        for (     int i=0;i<s.length (); i++         )        {            if (  s.charat (i) = = ' A ') {                num++;             }        }?

2. Using the properties of an array of bytes arrays:

? int num = 0;        Byte[] Bytes=s.getbytes ();        for (  int i=0;i<bytes.length;i++       )        {                        if (bytes[i]== ' a ') {                num++;             }        }?

3. Using the array char

?        Char[] C=s.tochararray ();           for (   int i=0;i<c.length;i++    )        {            if (    c[i]== ' a '    ) {                num++;             }        }?

4. Using the substring (startindex,endindex) string

? for (   int i=0;i<s.length (); i++  )        {            String s1=s.substring (i,i+1);            if (   s1.equals ("a")) {  //Note this place cannot be ' a '.                num++;             }        }?

The

Java implementation iterates through each letter of a string (summary)

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.