Why subsequence(a,b).toString() is faster than substring(a,b)?

728 views Asked by At

Why subsequence(a,b).toString() is faster than substring(a,b)? when i convert my all subsequences to substring it slows up to %7 all the time. why does it happen? Below is my code;

private static String filterStr(String s)
{
    for(int a = 0; a < s.length(); a++)
    {
        int c = s.charAt(a);
        if(((c < 65) || ((c >90) &&(c < 97)) || (c > 122)))
        {
            if(c!=34 && c!=96 && c!=39)// tırnak değillerse
            {
                String temp = s.substring(0,a);
                temp+= s.subSequence(a+1,s.length());
                s = temp;
                a--;
            }
            else 
            {
                if(a !=0) // if not at the beginning
                {   
                    if(a == s.length()-1)
                        s = s.subSequence(0,s.length()-1).toString();
                    else
                        s = s.subSequence(0,s.length()-2).toString();
                }
                else 
                    s = s.subSequence(1,s.length()).toString();
            }
        }
        if(c >= 65 && c <= 90) // convert to lower case first character.
        {
            String temp = s.substring(1,s.length());
            c+=32;
            s = (char)c + temp;
        }
    }
    return s;
}
1

There are 1 answers

3
Kamil.H On
CharSequence subSequence(int beginIndex, int endIndex) {
   return this.substring(beginIndex, endIndex);
}

this is the implementation of subSequence method, It can not be faster/slower.