将符号转换为字符串中的不同符号

假设我们有一个String myString = "abcde" 我们需要为整个字母表的每个字母分配一个其他符号,那么当一个字符串获取值时,它们会按照指示自动转换。示例:“a”变成“α”,“b”变成“β”,“c”变成“σ”,“d”变成“δ”,“e”变成“ε”

我们从哪里开始?


www说
浏览 237回答 3
3回答

冉冉说

有几种方法可以做到这一点,这里有两种:replaceAll()多次使用该方法 ( https://www.javatpoint.com/java-string-replaceall ) 它基本上允许您用其他内容替换字符串中每次出现的字符通过创建一个 Map 和一个函数,遍历 String 并用其对应的字符替换每个字符第一个更容易实现,但如果你有很多替换会更混乱,而第二个更容易实现和使用,但需要更多代码才能工作。就个人而言,我会将这两者结合起来,针对每个地图条目迭代一次:在一个循环中,对每个可替换字符for使用。replaceAll()

POPMUISE

您可以运行一个循环来执行此操作,如下所示:如果您的字符串很小并且字典可能包含更多值 -遍历字符串:Map<String,String> dict = new HashMap<>();dict.put("a", "α");dict.put("b", "β");// .... and so onfor (char c : myString.toCharArray()) {&nbsp; &nbsp; String currentChar = String.valueOf(c);&nbsp; &nbsp; if (dict.containsKey(currentChar))&nbsp; &nbsp; &nbsp; &nbsp; myString = myString.replaceAll(currentChar,dict.get(currentChar));}如果你的字符串很大,而字典是固定的(比如,只包含 az 字符) -遍历字典:Map<String,String> dict = new HashMap<>();dict.put("a", "α");dict.put("b", "β");// .... and so onfor (String key : dict.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; myString = myString.replaceAll(key,dict.get(key));}

喵喔喔

我已经写了一个类似的函数来将重音转换为相应的字母表,它可以帮助你public static String ChangeAccentToCorrespondingChar(String string) // at the end of this function we'll only get these elements "^[a-z0-9_-]*$"&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; HashMap<Integer,Integer> asciiMap = new HashMap<Integer,Integer>(){{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; put(192, 97); put(193, 97);&nbsp; put(194, 97);&nbsp; put(195, 97);&nbsp; put(196, 97);&nbsp; put(197, 97);&nbsp; 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);&nbsp; &nbsp; &nbsp; &nbsp; }};&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder changedString = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; char[] charArr = string.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0;i<charArr.length;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char character = charArr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int ascii = character;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (asciiMap.containsKey(ascii)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ascii = asciiMap.get(ascii);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; character = (char)ascii;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return changedString.toString();&nbsp; &nbsp; }在您的情况下,您想反向执行,将字母表示为重音,您可以简单地更改上面 Hashmap 中的 ascii 值来检索您的要求。在哈希映射中,键是原始字符的 ascii,值是新字符的 ascii。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java