What is an efficient way to compare StringBuilder objects

后端 未结 4 1047
无人共我
无人共我 2021-01-03 19:25

Well I have two StringBuilder objects, I need to compare them in Java. One way I know I can do is

sb1.toString().equals(sb2.toString());

bu

4条回答
  •  别那么骄傲
    2021-01-03 20:14

    Since Java 11, StringBuilder implements Comparable, so you can use a compareTo method for the equality test:

    System.out.println(sb1.compareTo(sb2) == 0);
    

提交回复
热议问题