Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 22 chars, tar = "__", tarLen = 2
- // "hello__there__lol__no_" -> "hello", "there", "lol", "no_"
- // | index 21
- // "hello____there__lol__no__" -> "hello", "", "there", "lol", "no", ""
- private static Boolean matchAt(String src, int srcLen, String tar, int tarLen, int i) {
- if ((i + tarLen) > srcLen) {
- return null;
- }
- for (int j = 1; j < tarLen; j++) {
- if (tar.charAt(j) != src.charAt(i + j)) {
- return Boolean.FALSE;
- }
- }
- return Boolean.TRUE;
- }
- /**
- * Splits the given string into an array, split by the given splitter string
- * @param string The search source string
- * @param split The splitter value
- * @return An array of strings
- * @author REghZy
- */
- @Nonnull
- public static String[] split_old(@Nullable String string, @Nullable String split) {
- int srcLen, tarLen, splitCount;
- if (string == null || (srcLen = string.length()) == 0)
- return EMPTY_STR_ARR;
- if (split == null || (tarLen = split.length()) == 0)
- return charToStringArray(string.toCharArray());
- if ((splitCount = countOccurrences(string, split)) == 0)
- return new String[]{string};
- String[] array = new String[splitCount + 1];
- char first = split.charAt(0);
- int k = 0, last = 0;
- for(int i = 0; i < string.length(); i++) {
- if (string.charAt(i) != first)
- continue;
- Boolean match = matchAt(string, srcLen, split, tarLen, i);
- if (match == null) {
- break;
- }
- else if (match) {
- array[k++] = string.substring(last, i);
- last = i + tarLen;
- i += (tarLen - 1);
- }
- }
- if (last <= srcLen)
- array[k] = string.substring(last);
- return array;
- }
- /**
- * Searches the given string for all occurrences of the given value
- * @param string The search source string
- * @param value The search value
- * @return The occurrence count of 'value' in 'string'
- * @author REghZy
- */
- public static int countOccurrences(String string, String value) {
- int count = 0, len = value.length(), index = -len;
- while ((index = string.indexOf(value, index + len)) != -1)
- ++count;
- return count;
- }
- /**
- * Splits the given string into an array, split by the given splitter string
- * @param string The search source string
- * @param split The splitter value
- * @return An array of strings
- * @author REghZy
- */
- @Nonnull
- public static String[] split(@Nullable String string, @Nullable String split) {
- int srcLen, tarLen, splitCount;
- if (string == null || (srcLen = string.length()) == 0)
- return EMPTY_STR_ARR;
- if (split == null || (tarLen = split.length()) == 0)
- return charToStringArray(string.toCharArray());
- if ((splitCount = countOccurrences(string, split)) == 0)
- return new String[]{string};
- String[] array = new String[splitCount + 1];
- char first = split.charAt(0);
- int i = -tarLen, last = 0, k = 0;
- while ((i = string.indexOf(split, i + tarLen)) != -1) {
- array[k++] = string.substring(last, i);
- last = i + tarLen;
- }
- if (last <= srcLen)
- array[k] = string.substring(last);
- return array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement