SoFunction
Updated on 2025-03-04

Java operation method for traversing strings and counting characters

1. Introduction

We have traversed the array before, and the so-called traversal is to obtain every element in the array.

Now it is the same when traversing strings, which means that we need to take out every character in the string.

2. Methods involved

public char charAt(int index): Return the corresponding character according to the index

Strings are also indexed, for example"Steel door 123 blows light snow"As an example, the corresponding indexes are0 1 2 3 4 5 6 7, we can find that this is exactly the same as our previous rules for array indexing!

public int length(): Returns the length of this string

Distinguishing - the length of the array:array name.length, the length of the array is a property, so when we call itlengthThere are no brackets afterwards. The length of the string is a method, when the method is calledlengthBrackets are required.

Code Example

package ;
import ;
public class StringDemo5 {
    public static void main(String[] args) {
        //1. Enter a string on the keyboard        Scanner sc = new Scanner();
        ("Please enter a string");
        String str = ();
        //2. Perform traversal        for (int i = 0; i < (); i++) {
            //i represents each index of the string in turn            char c = (i);
            (c);
        }
    }
}

3. Exercise: count the number of strings

Requirements: Enter a string on the keyboard to count the number of times the uppercase alphabetical characters, lowercase alphabetical characters, and numeric characters appear in the string (other characters are not considered)

package ;
import ;
public class StringDemo6 {
    public static void main(String[] args) {
        //1. Enter a string on the keyboard        Scanner sc = new Scanner();
        ("Please enter a string");
        String str = ();
        //2. Statistics--- Counter Thought        //Define three counters        int bigCount = 0;
        int smallCount = 0;
        int numberCount = 0;
        // If you need to count other characters, just add anotherCount        for (int i = 0; i < (); i++) {
            //i represents each index in the string in turn            char c = (i);
            if(c >= 'a' && c <= 'z'){
                // When char type variables are involved in calculation, they will automatically query the Ascii code table and become the corresponding number, and then compare them.                smallCount++;
            }else if(c >= 'A' && c <= 'Z'){
                bigCount++;
            // Note: If you write "c >= 0 && c <= 9", there will be a problem, because in the ASCII code table, the number corresponding to the character '0' is actually 48, and the number corresponding to the character '9' is actually 57.            }else if(c &gt;= '0' &amp;&amp; c &lt;= '9'){
                numberCount++;
            }
        }
        //3. Output printing        ("The lowercase letters have:" + smallCount + "indivual");
        ("Uppercase letters have:" + bigCount + "indivual");
        ("The letters of numbers are:" + numberCount + "indivual");
    }
}

This is the article about how to operate Java traversal strings and count characters. For more related Java traversal string content, please search for my previous article or continue browsing the following related articles. I hope everyone will support me in the future!