java 如何替换java字符串中的字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2653739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:13:47  来源:igfitidea点击:

How to replace characters in a java String?

javastring

提问by ManBugra

I like to replace a certain set of characters of a string with a corresponding replacement character in an efficent way.

我喜欢以有效的方式用相应的替换字符替换字符串的特定字符集。

For example:

例如:

String sourceCharacters = "??????????";
String targetCharacters = "sdccSDCCzZ";

String result = replaceChars("Gra?i??e", sourceCharacters , targetCharacters );

Assert.equals(result,"Gracisce") == true;

Is there are more efficient way than to use the replaceAllmethod of the String class?

有没有比使用replaceAllString 类的方法更有效的方法?

My first idea was:

我的第一个想法是:

final String s = "Gra?i??e";
String sourceCharacters = "??????????";
String targetCharacters = "sdccSDCCzZ";

// preparation
final char[] sourceString = s.toCharArray();
final char result[] = new char[sourceString.length];
final char[] targetCharactersArray = targetCharacters.toCharArray();

// main work
for(int i=0,l=sourceString.length;i<l;++i)
{
  final int pos = sourceCharacters.indexOf(sourceString[i]);
  result[i] = pos!=-1 ? targetCharactersArray[pos] : sourceString[i];
}

// result
String resultString = new String(result);

Any ideas?

有任何想法吗?

Btw, the UTF-8 characters are causing the trouble, with US_ASCII it works fine.

顺便说一句,UTF-8 字符导致了问题,使用 US_ASCII 可以正常工作。

回答by BalusC

You can make use of java.text.Normalizerand a shot of regex to get rid of the diacriticsof which there exist muchmore than you have collected as far.

您可以使用java.text.Normalizer正则表达式来消除变音符号,这些变音符号数量远远超过您目前收集的数量。

Here's an SSCCE, copy'n'paste'n'run it on Java 6:

这是一个SSCCE,在 Java 6 上复制'n'paste'n'run它:

package com.stackoverflow.q2653739;

import java.text.Normalizer;
import java.text.Normalizer.Form;

public class Test {

    public static void main(String... args) {
        System.out.println(removeDiacriticalMarks("Gra?i??e"));
    }

    public static String removeDiacriticalMarks(String string) {
        return Normalizer.normalize(string, Form.NFD)
            .replaceAll("\p{InCombiningDiacriticalMarks}+", "");
    }
}

This should yield

这应该产生

Gracisce

At least, it does here at Eclipse with console character encoding set to UTF-8 (Window > Preferences > General > Workspace > Text File Encoding). Ensure that the same is set in your environment as well.

至少,它在 Eclipse 中使用控制台字符编码设置为 UTF-8(Window > Preferences > General > Workspace > Text File Encoding)。确保在您的环境中也设置了相同的设置。

As an alternative, maintain a Map<Character, Character>:

作为替代方案,维护一个Map<Character, Character>

Map<Character, Character> charReplacementMap = new HashMap<Character, Character>();
charReplacementMap.put('?', 's');
charReplacementMap.put('?', 'd');
// Put more here.

String originalString = "Gra?i??e";
StringBuilder builder = new StringBuilder();

for (char currentChar : originalString.toCharArray()) {
    Character replacementChar = charReplacementMap.get(currentChar);
    builder.append(replacementChar != null ? replacementChar : currentChar);
}

String newString = builder.toString();

回答by Donal Fellows

I'd use the replacemethod in a simple loop.

我会replace在一个简单的循环中使用该方法。

String sourceCharacters = "??????????";
String targetCharacters = "sdccSDCCzZ";

String s = "Gra?i??e";
for (int i=0 ; i<sourceCharacters.length() ; i++)
    s = s.replace(sourceCharacters.charAt[i], targetCharacters.charAt[i]);

System.out.println(s);