Delete everything after part of a string

后端 未结 10 2150
时光取名叫无心
时光取名叫无心 2020-12-03 09:25

I have a string that is built out of three parts. The word I want the string to be (changes), a seperating part (doesn\'t change) and the last part which changes. I want to

10条回答
  •  温柔的废话
    2020-12-03 10:16

    I created Sample program for all the approches and SubString seems to be fastest one.

    Using builder : 54
    Using Split : 252
    Using Substring  : 10
    

    Below is the sample program code

                for (int count = 0; count < 1000; count++) {
            // For JIT
        }
        long start = System.nanoTime();
        //Builder
        StringBuilder builder = new StringBuilder(
                "Stack Overflow - A place to ask stuff");
        builder.delete(builder.indexOf("-"), builder.length());
        System.out.println("Using builder : " + (System.nanoTime() - start)
                / 1000);
        start = System.nanoTime();
        //Split
        String string = "Stack Overflow - A place to ask stuff";
        string.split("-");
        System.out.println("Using Split : " + (System.nanoTime() - start)
                / 1000);
        //SubString
        start = System.nanoTime();
        String string1 = "Stack Overflow - A place to ask stuff";
        string1.substring(0, string1.indexOf("-"));
        System.out.println("Using Substring : " + (System.nanoTime() - start)
                / 1000);
        return null;
    

提交回复
热议问题