smdbngkt
smdbngkt

Reputation: 11

How to replace String with unicode characters

I intend to replace strings with normal strings into split strings, but there is a difference in length between the normal strings which amounts to 62 and the split length turns out to be 117, so when we write the 'a' button it doesn't change to '𝕒' is there another way of writing replace string easier?

public static String doublestruck(String input){
    String normal = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String split = "πŸ˜πŸ™πŸšπŸ›πŸœπŸπŸžπŸŸπŸ πŸ‘π•’π•“π•”π••π•–π•—π•˜π•™π•šπ•›π•Ÿπ• π•‘π•’π•£π•€π•₯𝕦𝕧𝕨𝕩π•ͺπ•«π”Έπ”Ήβ„‚π”»π”Όπ”½π”Ύβ„π•€π•π•‚π•ƒπ•„β„•π•†β„™β„šβ„π•Šπ•‹π•Œπ•π•Žπ•π•β„€";

    String output = "";
    char letter;
    for(int i = 0; i < input.length(); i++){
        letter = input.charAt(i);
        int a = normal.indexOf(letter);
        output += (a != -1) ? split.charAt(a):letter;
    }
    return new StringBuilder(output).toString();
}

Upvotes: 0

Views: 75

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308031

The letters like 𝟜 (U+1D7DC) are not in the Basic Multilingual Pane and thus take up two char values in Java.

Instead of charAt you need to use codePointAt and to find the correct offset you need to use offsetByCodePoint instead of directly using the same index. So split.charAt(a) needs to be replaced by split.codePointAt(spli.offsetByCodePoint(0, a)).

Upvotes: 1

Related Questions