@@ -271,11 +271,11 @@ public static String trimLeadingWhitespace(String str) {
271
271
return str ;
272
272
}
273
273
274
- StringBuilder sb = new StringBuilder ( str ) ;
275
- while (sb .length () > 0 && Character .isWhitespace (sb .charAt (0 ))) {
276
- sb . deleteCharAt ( 0 ) ;
274
+ int beginIdx = 0 ;
275
+ while (beginIdx < str .length () && Character .isWhitespace (str .charAt (beginIdx ))) {
276
+ beginIdx ++ ;
277
277
}
278
- return sb . toString ( );
278
+ return str . substring ( beginIdx );
279
279
}
280
280
281
281
/**
@@ -289,11 +289,11 @@ public static String trimTrailingWhitespace(String str) {
289
289
return str ;
290
290
}
291
291
292
- StringBuilder sb = new StringBuilder ( str ) ;
293
- while (sb . length () > 0 && Character .isWhitespace (sb .charAt (sb . length () - 1 ))) {
294
- sb . deleteCharAt ( sb . length () - 1 ) ;
292
+ int endIdx = str . length () - 1 ;
293
+ while (endIdx >= 0 && Character .isWhitespace (str .charAt (endIdx ))) {
294
+ endIdx -- ;
295
295
}
296
- return sb . toString ( );
296
+ return str . substring ( 0 , endIdx + 1 );
297
297
}
298
298
299
299
/**
@@ -307,11 +307,11 @@ public static String trimLeadingCharacter(String str, char leadingCharacter) {
307
307
return str ;
308
308
}
309
309
310
- StringBuilder sb = new StringBuilder ( str ) ;
311
- while (sb .length () > 0 && sb . charAt ( 0 ) == leadingCharacter ) {
312
- sb . deleteCharAt ( 0 ) ;
310
+ int beginIdx = 0 ;
311
+ while (beginIdx < str .length () && leadingCharacter == str . charAt ( beginIdx ) ) {
312
+ beginIdx ++ ;
313
313
}
314
- return sb . toString ( );
314
+ return str . substring ( beginIdx );
315
315
}
316
316
317
317
/**
@@ -325,11 +325,11 @@ public static String trimTrailingCharacter(String str, char trailingCharacter) {
325
325
return str ;
326
326
}
327
327
328
- StringBuilder sb = new StringBuilder ( str ) ;
329
- while (sb . length () > 0 && sb . charAt ( sb . length () - 1 ) == trailingCharacter ) {
330
- sb . deleteCharAt ( sb . length () - 1 ) ;
328
+ int endIdx = str . length () - 1 ;
329
+ while (endIdx >= 0 && trailingCharacter == str . charAt ( endIdx ) ) {
330
+ endIdx -- ;
331
331
}
332
- return sb . toString ( );
332
+ return str . substring ( 0 , endIdx + 1 );
333
333
}
334
334
335
335
/**
0 commit comments