StringBuilderUtils { public static void trim(@NonNull StringBuilder stringBuilder) { while (stringBuilder.indexOf(SPACE) == 0) { stringBuilder.replace(0, 1, EMPTY); } while (stringBuilder.lastIndexOf(SPACE) == (stringBuilder.length() - 1) && stringBuilder.length() > 0) { stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), EMPTY); } }  static void replace(@NonNull StringBuilder stringBuilder,
                        @NonNull String toReplace,
                        @NonNull String replacement); static void trim(@NonNull StringBuilder stringBuilder); static boolean isEmpty(@NonNull StringBuilder stringBuilder); static boolean startsWith(@NonNull StringBuilder stringBuilder, @NonNull String start); static boolean contains(@NonNull StringBuilder stringBuilder, @NonNull String contains); static boolean equals(@NonNull StringBuilder stringBuilder, @NonNull String equal); @NonNull static StringBuilder substring(@NonNull StringBuilder stringBuilder, int start, int end);  }
@Test public void trim_LeadingAndTrailingSpaces() { StringBuilder stringBuilder = new StringBuilder(" t e s t "); StringBuilderUtils.trim(stringBuilder); Assert.assertEquals(stringBuilder.toString(), "t e s t"); }