如何在Java中替换字符串中的字符?

时间:2022-05-25 16:49:46

Using Java, I want to go through the lines of a text and replace all ampersand symbols (&) with the XML entity reference &.

使用Java,我想遍历文本的行并将所有的&符号(&)替换为XML实体引用&

I scan the lines of the text and then each word in the text with the Scanner class. Then I use the CharacterIterator to iterate over each characters of the word. However, how can I replace the character? First, Strings are immutable objects. Second, I want to replace a character (&) with several characters(amp&;). How should I approach this?

我用扫描器类扫描文本的行,然后扫描文本中的每个字。然后我使用characters iterator对单词的每个字符进行迭代。但是,我如何替换这个角色呢?首先,字符串是不可变的对象。其次,我想用几个字符(amp&;)替换一个字符(&)。我该怎么做呢?

CharacterIterator it = new StringCharacterIterator(token);
for(char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
       if(ch == '&') {

       }
}

10 个解决方案

#1


103  

Try using String.replace() or String.replaceAll() instead.

尝试使用String.replace()或String.replaceAll()代替。

String my_new_str = my_str.replace("&", "&");

(Both replace all occurrences; replaceAll allows use of regex.)

(替换所有事件;replaceAll允许使用regex。

#2


88  

The simple answer is:

简单的答案是:

token = token.replace("&", "&");

Despite the name as compared to replaceAll, replace does do a replaceAll, it just doesn't use a regular expression, which seems to be in order here (both from a performance and a good practice perspective - don't use regular expressions by accident as they have special character requirements which you won't be paying attention to).

尽管名字replaceAll相比,取代replaceAll,它只是不使用一个正则表达式,它似乎是在订单(从性能和良好实践的角度——不要使用正则表达式时偶然有特殊字符要求你不会关注)。

Sean Bright's answer is probably as good as is worth thinking about from a performance perspective absent some further target requirement on performance and performance testing, if you already know this code is a hot spot for performance, if that is where your question is coming from. It certainly doesn't deserve the downvotes. Just use StringBuilder instead of StringBuffer unless you need the synchronization.

Sean Bright的答案可能很好,值得从性能的角度考虑,因为在性能和性能测试方面没有进一步的目标需求,如果您已经知道这段代码是性能的热点,如果这就是您的问题所在的话。它当然不值得被降级。只需要使用StringBuilder而不是StringBuffer,除非您需要同步。

That being said, there is a somewhat deeper potential problem here. Escaping characters is a known problem which lots of libraries out there address. You may want to consider wrapping the data in a CDATA section in the XML, or you may prefer to use an XML library (including the one that comes with the JDK now) to actually generate the XML properly (so that it will handle the encoding).

话虽如此,这里有一个更深层次的潜在问题。转义字符是一个已知的问题,很多库都在那里。您可能想要考虑在XML的CDATA部分中包装数据,或者您可能更喜欢使用XML库(包括JDK附带的那个)来正确地生成XML(以便它能够处理编码)。

Apache also has an escaping library as part of Commons Lang.

Apache也有一个转义库作为Commons Lang的一部分。

#3


14  

StringBuilder s = new StringBuilder(token.length());

CharacterIterator it = new StringCharacterIterator(token);
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
    switch (ch) {
        case '&':
            s.append("&");
            break;
        case '<':
            s.append("&lt;");
            break;
        case '>':
            s.append("&gt;");
            break;
        default:
            s.append(ch);
            break;
    }
}

token = s.toString();

#4


7  

You may also want to check to make sure your not replacing an occurrence that has already been replaced. You can use a regular expression with negative lookahead to do this.

您可能还需要检查以确保没有替换已被替换的事件。你可以用一个带负号的正则表达式来做这个。

For example:

例如:

String str = "sdasdasa&amp;adas&dasdasa";
str = str.replaceAll("&(?!amp;)", "&amp;");

字符串str = " sdasdasa&adas&dasdasa”;str = str.replaceAll(“&(?,)”、“和”);

This would result in the string "sdasdasa&adas&dasdasa".

这将导致字符串“sdasdasa&adas&dasdasa”。

The regex pattern "&(?!amp;)" basically says: Match any occurrence of '&' that is not followed by 'amp;'.

regex模式“&(?)”基本上说:匹配任何“&”的出现,而不是“amp”。

#5


4  

Just create a string that contains all of the data in question and then use String.replaceAll() like below.

只需创建一个包含所有问题数据的字符串,然后使用string . replaceall()如下所示。

String result = yourString.replaceAll("&", "&amp;");

#6


2  

Try this code.You can replace any character with another given character. Here I tried to replace the letter 'a' with "-" character for the give string "abcdeaa"

试试这个代码。您可以用另一个给定的字符替换任何字符。在这里,我试图将字母“a”替换为给定字符串“abcdeaa”的“-”字符

OutPut -->_bcdef__

输出- - > _bcdef__

    public class Replace {

    public static void replaceChar(String str,String target){
        String result = str.replaceAll(target, "_");
        System.out.println(result);
    }

    public static void main(String[] args) {
        replaceChar("abcdefaa","a");
    }

}

#7


1  

Escaping strings can be tricky - especially if you want to take unicode into account. I suppose XML is one of the simpler formats/languages to escape but still. I would recommend taking a look at the StringEscapeUtils class in Apache Commons Lang, and its handy escapeXml method.

转义字符串可能比较棘手——尤其是如果您想考虑unicode的话。我认为XML是一种更简单的格式/语言,但仍然可以转义。我建议您看一下Apache Commons Lang中的StringEscapeUtils类,以及它的方便的escapeXml方法。

#8


0  

If you're using Spring you can simply call HtmlUtils.htmlEscape(String input) which will handle the '&' to '&' translation.

如果使用Spring,可以简单地调用HtmlUtils。htmlEscape(字符串输入),它将处理“&”到“&”的翻译。

#9


0  

Have a look at this method.

看看这个方法。

#10


0  

//I think this will work, you don't have to replace on the even, it's just an example. 

 public void emphasize(String phrase, char ch)
    {
        char phraseArray[] = phrase.toCharArray(); 
        for(int i=0; i< phrase.length(); i++)
        {
            if(i%2==0)// even number
            {
                String value = Character.toString(phraseArray[i]); 
                value = value.replace(value,"*"); 
                phraseArray[i] = value.charAt(0);
            }
        }
    }

#1


103  

Try using String.replace() or String.replaceAll() instead.

尝试使用String.replace()或String.replaceAll()代替。

String my_new_str = my_str.replace("&", "&amp;");

(Both replace all occurrences; replaceAll allows use of regex.)

(替换所有事件;replaceAll允许使用regex。

#2


88  

The simple answer is:

简单的答案是:

token = token.replace("&", "&amp;");

Despite the name as compared to replaceAll, replace does do a replaceAll, it just doesn't use a regular expression, which seems to be in order here (both from a performance and a good practice perspective - don't use regular expressions by accident as they have special character requirements which you won't be paying attention to).

尽管名字replaceAll相比,取代replaceAll,它只是不使用一个正则表达式,它似乎是在订单(从性能和良好实践的角度——不要使用正则表达式时偶然有特殊字符要求你不会关注)。

Sean Bright's answer is probably as good as is worth thinking about from a performance perspective absent some further target requirement on performance and performance testing, if you already know this code is a hot spot for performance, if that is where your question is coming from. It certainly doesn't deserve the downvotes. Just use StringBuilder instead of StringBuffer unless you need the synchronization.

Sean Bright的答案可能很好,值得从性能的角度考虑,因为在性能和性能测试方面没有进一步的目标需求,如果您已经知道这段代码是性能的热点,如果这就是您的问题所在的话。它当然不值得被降级。只需要使用StringBuilder而不是StringBuffer,除非您需要同步。

That being said, there is a somewhat deeper potential problem here. Escaping characters is a known problem which lots of libraries out there address. You may want to consider wrapping the data in a CDATA section in the XML, or you may prefer to use an XML library (including the one that comes with the JDK now) to actually generate the XML properly (so that it will handle the encoding).

话虽如此,这里有一个更深层次的潜在问题。转义字符是一个已知的问题,很多库都在那里。您可能想要考虑在XML的CDATA部分中包装数据,或者您可能更喜欢使用XML库(包括JDK附带的那个)来正确地生成XML(以便它能够处理编码)。

Apache also has an escaping library as part of Commons Lang.

Apache也有一个转义库作为Commons Lang的一部分。

#3


14  

StringBuilder s = new StringBuilder(token.length());

CharacterIterator it = new StringCharacterIterator(token);
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
    switch (ch) {
        case '&':
            s.append("&amp;");
            break;
        case '<':
            s.append("&lt;");
            break;
        case '>':
            s.append("&gt;");
            break;
        default:
            s.append(ch);
            break;
    }
}

token = s.toString();

#4


7  

You may also want to check to make sure your not replacing an occurrence that has already been replaced. You can use a regular expression with negative lookahead to do this.

您可能还需要检查以确保没有替换已被替换的事件。你可以用一个带负号的正则表达式来做这个。

For example:

例如:

String str = "sdasdasa&amp;adas&dasdasa";
str = str.replaceAll("&(?!amp;)", "&amp;");

字符串str = " sdasdasa&adas&dasdasa”;str = str.replaceAll(“&(?,)”、“和”);

This would result in the string "sdasdasa&adas&dasdasa".

这将导致字符串“sdasdasa&adas&dasdasa”。

The regex pattern "&(?!amp;)" basically says: Match any occurrence of '&' that is not followed by 'amp;'.

regex模式“&(?)”基本上说:匹配任何“&”的出现,而不是“amp”。

#5


4  

Just create a string that contains all of the data in question and then use String.replaceAll() like below.

只需创建一个包含所有问题数据的字符串,然后使用string . replaceall()如下所示。

String result = yourString.replaceAll("&", "&amp;");

#6


2  

Try this code.You can replace any character with another given character. Here I tried to replace the letter 'a' with "-" character for the give string "abcdeaa"

试试这个代码。您可以用另一个给定的字符替换任何字符。在这里,我试图将字母“a”替换为给定字符串“abcdeaa”的“-”字符

OutPut -->_bcdef__

输出- - > _bcdef__

    public class Replace {

    public static void replaceChar(String str,String target){
        String result = str.replaceAll(target, "_");
        System.out.println(result);
    }

    public static void main(String[] args) {
        replaceChar("abcdefaa","a");
    }

}

#7


1  

Escaping strings can be tricky - especially if you want to take unicode into account. I suppose XML is one of the simpler formats/languages to escape but still. I would recommend taking a look at the StringEscapeUtils class in Apache Commons Lang, and its handy escapeXml method.

转义字符串可能比较棘手——尤其是如果您想考虑unicode的话。我认为XML是一种更简单的格式/语言,但仍然可以转义。我建议您看一下Apache Commons Lang中的StringEscapeUtils类,以及它的方便的escapeXml方法。

#8


0  

If you're using Spring you can simply call HtmlUtils.htmlEscape(String input) which will handle the '&' to '&' translation.

如果使用Spring,可以简单地调用HtmlUtils。htmlEscape(字符串输入),它将处理“&”到“&”的翻译。

#9


0  

Have a look at this method.

看看这个方法。

#10


0  

//I think this will work, you don't have to replace on the even, it's just an example. 

 public void emphasize(String phrase, char ch)
    {
        char phraseArray[] = phrase.toCharArray(); 
        for(int i=0; i< phrase.length(); i++)
        {
            if(i%2==0)// even number
            {
                String value = Character.toString(phraseArray[i]); 
                value = value.replace(value,"*"); 
                phraseArray[i] = value.charAt(0);
            }
        }
    }