字符串:如何用单个字符替换多个可能的字符?

时间:2023-01-22 16:52:08

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...

toLowerCase()就在那里因为我想要它也是低壳的......

3 个解决方案

#1


64  

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.

replaceAll正在使用正则表达式,并使用。在一个字符类[]里面只识别一个。而不是任何角色。

#2


10  

s.replaceAll("[\\s\\.]", "_")

#3


4  

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

使用String#replace()而不是String#replaceAll(),您不需要正则表达式来替换单字符。

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

使用replaceAll()两次:100274

using replaceAll() once: 24814

使用replaceAll()一次:24814

using replace() twice: 31642

使用replace()两次:31642

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

当然,我没有对应用程序进行内存消耗分析,这可能会产生非常不同的结果。

#1


64  

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.

replaceAll正在使用正则表达式,并使用。在一个字符类[]里面只识别一个。而不是任何角色。

#2


10  

s.replaceAll("[\\s\\.]", "_")

#3


4  

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

使用String#replace()而不是String#replaceAll(),您不需要正则表达式来替换单字符。

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

使用replaceAll()两次:100274

using replaceAll() once: 24814

使用replaceAll()一次:24814

using replace() twice: 31642

使用replace()两次:31642

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

当然,我没有对应用程序进行内存消耗分析,这可能会产生非常不同的结果。