How to replace a char with a string

You can use a StringBuilder to do this. Given that you have one array with your chars, you can simply iterate over each char and append its translated variant to the StringBuilder object.

Example:

    char[] chars = {'h', 'e', 'l', 'l', 'o'};
    StringBuilder sb = new StringBuilder();
    for (char c : chars) {
        sb.append(getMorse(c));
    }
    System.out.println(sb.toString());

Where getMorse() is a function which returns a String containing the morse code variant of the char.

Leave a Comment