anthony restaino
8 years ago
2 changed files with 110 additions and 45 deletions
@ -0,0 +1,99 @@
@@ -0,0 +1,99 @@
|
||||
package acr.browser.lightning.utils; |
||||
|
||||
import android.support.annotation.NonNull; |
||||
|
||||
/** |
||||
* A collection of utils methods for |
||||
* {@link StringBuilder} that provides |
||||
* API equality with the {@link String} |
||||
* API. |
||||
*/ |
||||
public class StringBuilderUtils { |
||||
|
||||
private static final String SPACE = " "; |
||||
private static final String EMPTY = ""; |
||||
|
||||
/** |
||||
* Replace a string in a string |
||||
* builder with another string. |
||||
* |
||||
* @param stringBuilder the string builder. |
||||
* @param toReplace the string to replace. |
||||
* @param replacement the replacement string. |
||||
*/ |
||||
public static void replace(@NonNull StringBuilder stringBuilder, |
||||
@NonNull String toReplace, |
||||
@NonNull String replacement) { |
||||
int index = stringBuilder.indexOf(toReplace); |
||||
if (index >= 0) { |
||||
stringBuilder.replace(index, index + toReplace.length(), replacement); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Trims a string builder of |
||||
* any spaces at the beginning |
||||
* and end. |
||||
* |
||||
* @param stringBuilder the string builder. |
||||
*/ |
||||
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.replace(stringBuilder.length() - 1, stringBuilder.length(), EMPTY); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Determines if the string builder is empty. |
||||
* |
||||
* @param stringBuilder the string builder. |
||||
* @return true if the string builder is empty, |
||||
* false otherwise. |
||||
*/ |
||||
public static boolean isEmpty(@NonNull StringBuilder stringBuilder) { |
||||
return stringBuilder.length() == 0; |
||||
} |
||||
|
||||
/** |
||||
* Determines if a string builder starts with |
||||
* a specific string. |
||||
* |
||||
* @param stringBuilder the string builder. |
||||
* @param start the starting string. |
||||
* @return true if the string builder starts |
||||
* with the string, false otherwise. |
||||
*/ |
||||
public static boolean startsWith(@NonNull StringBuilder stringBuilder, @NonNull String start) { |
||||
return stringBuilder.indexOf(start) == 0; |
||||
} |
||||
|
||||
/** |
||||
* Determines if a string builder contains a string. |
||||
* |
||||
* @param stringBuilder the string builder. |
||||
* @param contains the string that it might contain. |
||||
* @return true if the string builder contains the |
||||
* string, false otherwise. |
||||
*/ |
||||
public static boolean contains(@NonNull StringBuilder stringBuilder, @NonNull String contains) { |
||||
return stringBuilder.indexOf(contains) >= 0; |
||||
} |
||||
|
||||
/** |
||||
* Determines equality between a string |
||||
* builder and a string. |
||||
* |
||||
* @param stringBuilder the string builder. |
||||
* @param equal the string. |
||||
* @return true if the string represented by |
||||
* the string builder is equal to the string. |
||||
*/ |
||||
public static boolean equals(@NonNull StringBuilder stringBuilder, @NonNull String equal) { |
||||
int index = stringBuilder.indexOf(equal); |
||||
return index >= 0 && stringBuilder.length() == equal.length(); |
||||
} |
||||
} |
Loading…
Reference in new issue