String: How to replace multiple possible characters with a single character?

I would like to replace all ‘.’ and ‘ ‘ with a ‘_’

but I don’t like my code…

is there a more efficient way to do this than:

String new_s = s.toLowerCase().replaceAll(" ", "_").replaceAll(".","_");

?

toLowerCase() just there because I want it lower-cased as well…

How do I replace a multiple sets of characters with a single character

I want to replace multiple sets of characters with a single/multiple characters. example : input i want hello RRRRRRRRRRRR My name RRR is RRRRRRRR arvind RR output i want hello A My name A is A a

how to replace multiple characters in a string?

how to replace multiple characters in a string? please help to fix the script I need to in the line name special characters have been replaced by the phrase special char newName = replace(name,

Replace consecutive characters with same single character

I was just wondering if there is a simple way of doing this. i.e. Replacing the occurrence of consecutive characters with the same character. For eg: – if my string is something likeeeee tttthhiiisss

Replacing all characters of a given string with a specific single character

I need to replace all characters of a String with a single character. For example, A string like ABCDEF is given. The replacement operation should produce the output like ######. All six characters in

How can i replace many special character including white space with single character?

How can i replace many special character including white space with single character?

How to replace characters in a string?

I want to replace characters in a string content in file. Below Dictionary shows the Key as unwanted character and i need to replace with the value in the Dictionary. Dictionary<string, string>

Find all possible versions of a string whose characters may have multiple variants

I’m looking for some tips on how to find all possible versions of a string whose characters may have multiple variants. A simple example: Macao is the starting string. The character ‘a’ has the vari

C# – Count string length and replace each character with another

How can I count the number of characters within a string and create another string with the same number of characters but replace all of them with a single character such as *? Thank you.

How to replace multiple spaces with single space?

I am developing web app using C#. I want to replace multiple spaces with single space in between string. I tried with normal string replace function, but it was not helpful. It is possible with Regula

Replace multiple characters in a string with one char

What the best way to replace multiple characters in a string with one char? string str(1 1 1); //out: 1 1 1

Answers

String new_s = s.toLowerCase().replaceAll("[ .]", "_");

EDIT:

replaceAll is using regular expressions, and using . inside a character class [ ] just recognises a . rather than any character.

Use String#replace() instead of String#replaceAll(), you don’t need regex for single-character replacement.

I created the following class to test what’s faster, give it a try:

public class NewClass {

    static String s = "some_string with spaces _and underlines";
    static int nbrTimes = 10000000;

    public static void main(String... args) {

        long start = new Date().getTime();
        for (int i = 0; i < nbrTimes; i++)
            doOne();
        System.out.println("using replaceAll() twice: " + (new Date().getTime() - start));



        long start2 = new Date().getTime();
        for (int i = 0; i < nbrTimes; i++)
            doTwo();
        System.out.println("using replaceAll() once: " + (new Date().getTime() - start2));

        long start3 = new Date().getTime();
        for (int i = 0; i < nbrTimes; i++)
            doThree();
        System.out.println("using replace() twice: " + (new Date().getTime() - start3));

    }

    static void doOne() {
        String new_s = s.toLowerCase().replaceAll(" ", "_").replaceAll(".", "_");
    }

    static void doTwo() {
        String new_s2 = s.toLowerCase().replaceAll("[ .]", "_");
    }

    static void doThree() {
        String new_s3 = s.toLowerCase().replace(" ", "_").replace(".", "_");
    }
}

I get the following output:

using replaceAll() twice: 100274

using replaceAll() once: 24814

using replace() twice: 31642

Of course I haven’t profiled the app for memory consumption, that might have given very different results.

s.replaceAll(“[//s//.]”, “_”)

You can use the split method, that’s follow a regular expression (regex).

Can you see examples here: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29