Is String.replace implementation really efficient?

后端 未结 3 886
青春惊慌失措
青春惊慌失措 2021-02-13 03:55

I used to think that String.replace is faster than String.replaceAll because the latter uses Pattern regex and the former does not. But in fact there is no significant differenc

3条回答
  •  情书的邮戳
    2021-02-13 04:31

    String replace and replace all method there is no diffrence in effiency or time both take n operation to replace the charecter following code may help you how string replace method work in java you can checkout in your eclipse

    package com.jav.exec;
    
    public class replace {
    
        static String s1="This the India the india the nation";
    
        public static String replace(String s2,String s3)
        {
            int counter=0;
            int count=s3.length()-1;
            int count1=0;
            char[] ca1=s1.toCharArray();
            char[] ca2=s2.toCharArray();
            char[] ca3=s3.toCharArray();
    
            for(int f1=0;f10)
                    {   
                        ca4[f-counter]=ca1[count2-counter];
                        counter--;
                    }
                }
                count2++;
            }
            return new String(ca4);
        }
    
        public static void main(String[] args)
        {
            System.out.println(replace("the","mother"));
        }
    }
    

提交回复
热议问题