Java删除字符串中的半角空格和全角空格

【前言】由于tirm()方法只能删除字符串前后的半角空格(廋空格),对中间的空格没办法,对随便哪里的全角空格(胖空格)也没办法,只能“手撕”了,然后就研究了以下四种不同方法的效率。

【代码】

public class DeleteSpaceTest {
    public static void main(String[] args) {
        String str = "火之国 木叶村 第四代火影 波风  水门";

        int totalTime = 5000000;
        System.out.println("totalTime=" + totalTime + "");

        long t1 = System.currentTimeMillis();
        deleteSpaceTestByReplaceAllSpace(str, totalTime);
        System.out.println("[deleteSpaceTestByReplaceAllSpace]"
                + String.valueOf(System.currentTimeMillis() - t1) + "ms");

        long t2 = System.currentTimeMillis();
        deleteSpaceTestByReplaceAllRegex(str, totalTime);
        System.out.println("[deleteSpaceTestByReplaceAllRegex]"
                + String.valueOf(System.currentTimeMillis() - t2) + "ms");

        long t3 = System.currentTimeMillis();
        deleteSpaceTestByJudgeACSII(str, totalTime);
        System.out.println("[deleteSpaceTestByJudgeACSII]"
                + String.valueOf(System.currentTimeMillis() - t3) + "ms");

        long t4 = System.currentTimeMillis();
        deleteSpaceTestByCharacter(str, totalTime);
        System.out.println("[deleteSpaceTestByCharacter]"
                + String.valueOf(System.currentTimeMillis() - t4) + "ms");

    }

    private static void deleteSpaceTestByCharacter(String str, int totalTimes) {
        for (int times = 0; times < totalTimes; times++) {
            StringBuilder sb = new StringBuilder();
            char[] chars = str.toCharArray();
            for (char c : chars) {
                if (Character.isSpaceChar(c)) {
                    continue;
                }
                sb.append(c);
            }
            String value = sb.toString();
            // System.out.println("[deleteSpaceTestByCharacter]value=" + value);
        }
    }

    private static void deleteSpaceTestByJudgeACSII(String str, int totalTimes) {
        for (int times = 0; times < totalTimes; times++) {
            StringBuilder sb = new StringBuilder();
            char[] chars = str.toCharArray();
            for (char c : chars) {
                if (32 == c || 12288 == c) {
                    continue;
                }
                sb.append(c);
            }
            String value = sb.toString();
            // System.out.println("[deleteSpaceTestByJudgeACSII]value=" + value);
        }
    }

    private static void deleteSpaceTestByReplaceAllRegex(String str, int totalTimes) {
        for (int times = 0; times < totalTimes; times++) {
            String value = str.replaceAll("[ | ]", "");
            // System.out.println("[deleteSpaceTestByReplaceAllRegex]value=" + value);
        }
    }

    private static void deleteSpaceTestByReplaceAllSpace(String str, int totalTimes) {
        for (int times = 0; times < totalTimes; times++) {
            String value = str.replaceAll(" ", "").replaceAll(" ", "");
            // System.out.println("[deleteSpaceTestByReplaceAllSpace]value=" + value);
        }
    }

}

【验证正确性-结果】

totalTime=1
[deleteSpaceTestByReplaceAllSpace]value=火之国木叶村第四代火影波风水门
[deleteSpaceTestByReplaceAllSpace]0ms
[deleteSpaceTestByReplaceAllRegex]value=火之国木叶村第四代火影波风水门
[deleteSpaceTestByReplaceAllRegex]1ms
[deleteSpaceTestByJudgeACSII]value=火之国木叶村第四代火影波风水门
[deleteSpaceTestByJudgeACSII]0ms
[deleteSpaceTestByCharacter]value=火之国木叶村第四代火影波风水门
[deleteSpaceTestByCharacter]1ms

【测试效率-结果】

测试一百次

totalTime=100
[deleteSpaceTestByReplaceAllSpace]5ms
[deleteSpaceTestByReplaceAllRegex]4ms
[deleteSpaceTestByJudgeACSII]1ms
[deleteSpaceTestByCharacter]2ms

测试五百次

totalTime=500
[deleteSpaceTestByReplaceAllSpace]14ms
[deleteSpaceTestByReplaceAllRegex]10ms
[deleteSpaceTestByJudgeACSII]1ms
[deleteSpaceTestByCharacter]7ms

测试一千次:

totalTime=1000
[deleteSpaceTestByReplaceAllSpace]19ms
[deleteSpaceTestByReplaceAllRegex]16ms
[deleteSpaceTestByJudgeACSII]3ms
[deleteSpaceTestByCharacter]7ms

测试一万次

totalTime=10000
[deleteSpaceTestByReplaceAllSpace]63ms
[deleteSpaceTestByReplaceAllRegex]60ms
[deleteSpaceTestByJudgeACSII]10ms
[deleteSpaceTestByCharacter]19ms

测试十万次

totalTime=100000
[deleteSpaceTestByReplaceAllSpace]193ms
[deleteSpaceTestByReplaceAllRegex]114ms
[deleteSpaceTestByJudgeACSII]34ms
[deleteSpaceTestByCharacter]49ms

测试一百万次

totalTime=1000000
[deleteSpaceTestByReplaceAllSpace]1214ms
[deleteSpaceTestByReplaceAllRegex]1000ms
[deleteSpaceTestByJudgeACSII]120ms
[deleteSpaceTestByCharacter]198ms

测试五百万次

totalTime=5000000
[deleteSpaceTestByReplaceAllSpace]4632ms
[deleteSpaceTestByReplaceAllRegex]3945ms
[deleteSpaceTestByJudgeACSII]504ms
[deleteSpaceTestByCharacter]849ms

【结论】

对于测试次数较少的情况来说,几十毫秒级别,差距不太,基本无感,最优为判断ACSII;对于百万级情况来说,replaceAll()方法耗时三四秒,判断ACSII和使用Character.isSpaceChar()判断一秒以内,但最优还是判断ACSII。综上所述,最优为判断ACSII(代码中deleteSpaceTestByJudgeACSII()方法)

【注】仅供参考 0(n_n)0

你可能感兴趣的:(Java,java,字符串)