简体   繁体   中英

Converting symbols to different symbols in a string

Assuming that we have a String myString = "abcde" We need to assign an other symbol for each letter of the entire alphabet, so when a string gets values, they are automatically converted as instructed. Example: "a" becomes "α", "b" becomes "β", "c" becomes "σ", "d" becomes "δ", "e" becomes "ε"

Where do we even begin?

There are several ways to do so, here are two :

  • Using the replaceAll() method several times ( https://www.javatpoint.com/java-string-replaceall ) which basically allows you to replace each occurrence of a character in a string by something else
  • By creating a Map and a function, to iterate through the String and replace each character by its corresponding one

The first is easier to implement but will be messier if you have lots of substitutions, while the second is cleaner to implement and use but requires more code to work.

Personally, I would combine these two to iterate once per map entry : in a for loop, use replaceAll() for each replaceable character.

You can run a loop to do this as shown below :

  • If your string is small and dictionary might contain more values - loop through string :

     Map<String,String> dict = new HashMap<>(); dict.put("a", "α"); dict.put("b", "β"); // .... and so on for (char c : myString.toCharArray()) { String currentChar = String.valueOf(c); if (dict.containsKey(currentChar)) myString = myString.replaceAll(currentChar,dict.get(currentChar)); } 
  • If your string is large, while dictionary is fixed (like, contains only az characters) - loop through dictionary :

     Map<String,String> dict = new HashMap<>(); dict.put("a", "α"); dict.put("b", "β"); // .... and so on for (String key : dict.keySet()) { myString = myString.replaceAll(key,dict.get(key)); } 

I already wrote a similar function to convert accents to corresponding alphabets, It can help you

public static String ChangeAccentToCorrespondingChar(String string) // at the end of this function we'll only get these elements "^[a-z0-9_-]*$"
    {
        HashMap<Integer,Integer> asciiMap = new HashMap<Integer,Integer>(){{
            put(192, 97); put(193, 97);  put(194, 97);  put(195, 97);  put(196, 97);  put(197, 97);  put(224, 97); put(225, 97); put(226, 97); put(227, 97); put(228, 97); put(229, 97); put(257, 97); put(256, 97); put(170, 97); put(131, 102); put(142, 122); put(158, 122); put(159, 121); put(221, 121); put(253, 121); put(255, 121); put(161, 105); put(204, 105); put(205, 105); put(206, 105); put(207, 105); put(236, 105); put(237, 105); put(238, 105); put(239, 105); put(299, 105); put(162, 99); put(169, 99); put(199, 99); put(231, 99); put(174, 114); put(200, 101); put(201, 101); put(202, 101); put(203, 101); put(232, 101); put(233, 101); put(234, 101); put(235, 101); put(275, 101); put(208, 100); put(209, 110); put(241, 110); put(210,111); put(211,111); put(212,111); put(213,111); put(214,111); put(216, 111); put(240, 111); put(242, 111); put(243, 111); put(244, 111); put(245, 111); put(246, 111); put(248, 111); put(333, 111); put(215, 120); put(217, 117); put(218, 117); put(219, 117); put(220, 117); put(249, 117); put(250, 117); put(251, 117); put(252, 117); put(254, 98);
        }};

        StringBuilder changedString = new StringBuilder();

        char[] charArr = string.toCharArray();
        for (int i=0;i<charArr.length;i++) {
            char character = charArr[i];
            int ascii = character;
            if (asciiMap.containsKey(ascii)) {
                ascii = asciiMap.get(ascii);
                character = (char)ascii;
            } 

        return changedString.toString();
    }

In your case you want to do in reverse, mean alphabets to its accent, you can simply change the ascii values in above Hashmap to retrieve your requirements. In hash map, keys are the original character's ascii and values are the new character ascii.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM