Question

How could I replace the letters in a String such as "Hello", with the letters here?

String bubbled = "ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏ";

I was initially just doing a replaceAll ("a","ⓐ"), but I feel like there has to be a more efficient way of doing this.

Was it helpful?

Solution 2

You could use the characters a and to determine the offset values for the alphabet. In combination with a StringBuilder it should be rather efficient. Of course you would likely have to be very strict about the input string being only alphabet characters.

This is the code for what I described above:

public class Bubbled {
    public static void main(String[] args) {
        char bubbledA = 'ⓐ';
        int lowerCaseOffset = bubbledA - 'a';
        int upperCaseOffset = bubbledA - 'A';

        String input = "Hello";
        StringBuilder bubbledOutput = new StringBuilder();
        for (Character c : input.toCharArray()) {
            if (Character.isUpperCase(c)) {
                bubbledOutput.append((char)(c + upperCaseOffset));
            } else {
                bubbledOutput.append((char)(c + lowerCaseOffset));
            }
        }

        System.out.println(bubbledOutput.toString());
    }
}

Output

ⓗⓔⓛⓛⓞ

OTHER TIPS

Split bubbled into lowercase and uppercase. Make a new StringBuilder, iterate over each char of source, and if chr >= 'a' && chr <= 'z' append lowercaseBubbled[chr - 'a'], if it's in uppercase range do similar, else just append chr. At the end, use toString on the builder.

Or you could use a slightly less efficient method, replaceChars (since it has to use indexOf) found in Apache Commons. Pro: it's a library, so no extra work for you.

Here is a code snipp that does it. It wont create a zillion String objects. I have only a smaller set of bubble chars just for demo purpose. Please tweak to your liking and no error handling has been done.

public class StackOverFlow {
    private static int[] bubbled = {'ⓐ', 'ⓑ', 'ⓒ', 'ⓓ', 'ⓔ'};
    private static int [] plain = {'a', 'b', 'c', 'd', 'e'};

    private static Map<Integer, Integer> charMap = new HashMap<>();

    public static void main(String[] args) {
        String test = "adcbbceead";
        for(int i=0; i<plain.length; i++) {
            charMap.put(plain[i], bubbled[i]);
        }
        replaceWithBuffer(test);
    }

    private static void replaceWithBuffer(String test) {
        System.out.println("Oginal String = " + test);
        StringBuilder sb = new StringBuilder(test);

        for(int i =0; i<test.length(); i++) {
            int ch = sb.charAt(i);
            char buubledChar = (char)charMap.get(ch).intValue();
            sb.setCharAt(i, buubledChar);
        }
        System.out.println("New String = " + sb.toString());
    }
}

Output:

enter image description here

Hope this helps :)

Use a for loop:

for (char i='a';i<'z';i++)
    str = str.replaceAll(i,bubbled[i-'a']);
for (char i='A';i<'Z';i++)
    str = str.replaceAll(i,bubbled[i-'A'+26]);

Of course, this wouldn't be too efficient, since Strings are immutable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top