How do I get char value of a string at a specified position?

The chartAt() method of the String class returns the char value at the specified index. An index ranges from 0 to length() - 1. If we specified an index beyond of this range a StringIndexOutOfBoundsException exception will be thrown.

These method use zero based index which means the first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

package org.kodejava.example.lang;

public class CharAtExample {
    public static void main(String[] args) {
        String[] colors = {"black", "white", "brown", "green", "yellow", "blue"};

        for (String color : colors) {
            // Get char value of a string at index number 3. Because
            // the index is zero based we'll get the fourth character
            // of each color in the array.
            char value = color.charAt(3);
            System.out.printf("The fourth char of %s is '%s'.%n", color, value);
        }

    }
}

Here is the program output:

The fourth char of black is 'c'
The fourth char of white is 't'
The fourth char of brown is 'w'
The fourth char of green is 'e'
The fourth char of yellow is 'l'
The fourth char of blue is 'e'

How do I replace characters in string?

package org.kodejava.example.lang;

public class StringReplace {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog";
        System.out.println("Before: " + text);

        // The replace method replace all occurrences of character
        // 'o' with 'u' and returns a new string object.
        text = text.replace('o', 'u');
        System.out.println("After : " + text);
    }
}

The result of the code snippet:

Before: The quick brown fox jumps over the lazy dog
After : The quick bruwn fux jumps uver the lazy dug

How do I iterate each characters of a string?

package org.kodejava.example.text;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class StringCharacterIteratorExample {
    private static final String text =
        "The quick brown fox jumps over the lazy dog";

    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text);

        int vowels = 0;
        int consonants = 0;

        // Iterates charater sets from the beginning to the last character
        for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowels = vowels + 1;
            } else if (ch != ' ') {
                consonants = consonants + 1;
            }
        }

        System.out.println("Number of vowels: " + vowels);
        System.out.println("Number of consonants: " + consonants);
    }
}

How do I create a repeated sequence of character?

This example show you how to create a repeated sequence of characters. To do this we use the Arrays.fill() method. This method fills an array of char with a character.

package org.kodejava.example.util;

import java.util.Arrays;

public class RepeatCharacterExample {
    public static void main(String[] args) {
        char c = 'x';
        int length = 10;

        // creates char array with 10 elements
        char[] chars = new char[length];

        // fill each element of chars array with 'x'
        Arrays.fill(chars, c);

        // print out the repeated 'x'
        System.out.println(String.valueOf(chars));
    }
}

As the result you get the x character repeated 10 times.

xxxxxxxxxx

For one liner code you can use the following code snippet, which will give you the same result.

new String(new char[10]).replace('\0', 'x');

Or

String.join("", Collections.nCopies(10, "x"));

How do I know if a character is uppercase?

In the previous example How do I know if a character is lowercase?, we’ve learned how to use the Character.isLowerCase() method. Now we will learn how to use the other method, which is the Character.isUpperCase() to determine if a letter is in an uppercase form. This method also return a boolean value that indicates whether the character is in uppercase form or not.

Another way to check if a letter is in uppercase form is by comparing the type of the characters, that can be obtained using the Character.getType() method with a defined constant value Character.UPPERCASE_LETTER.

Below is the code snippet that demonstrate this methods.

package org.kodejava.example.lang;

public class CharacterIsUperCaseExample {
    public static void main(String[] args) {
        char a = 'A';

        // Checks to see if a letter is a uppercase letter.
        if (Character.isUpperCase(a)) {
            System.out.println(a + " is an uppercase character.");
        }

        // Checks to see if a letter is an uppercase letter
        // by comparing the character type against
        // Character.UPPERCASE_LETTER constant value.
        int charType = Character.getType(a);
        if (charType == Character.UPPERCASE_LETTER) {
            System.out.println(a + " is an uppercase character.");
        }
    }
}

The above example give you the following result when it executed:

A is an uppercase character.
A is an uppercase character.