Advertisement
bobmarley12345

Java fast split(String, String) into arrays

Dec 24th, 2022 (edited)
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. // 22 chars, tar = "__", tarLen = 2
  2. // "hello__there__lol__no_"    -> "hello", "there", "lol", "no_"
  3. //                       | index 21
  4. // "hello____there__lol__no__" -> "hello", "", "there", "lol", "no", ""
  5. private static Boolean matchAt(String src, int srcLen, String tar, int tarLen, int i) {
  6.     if ((i + tarLen) > srcLen) {
  7.         return null;
  8.     }
  9.     for (int j = 1; j < tarLen; j++) {
  10.         if (tar.charAt(j) != src.charAt(i + j)) {
  11.             return Boolean.FALSE;
  12.         }
  13.     }
  14.     return Boolean.TRUE;
  15. }
  16.  
  17. /**
  18.  * Splits the given string into an array, split by the given splitter string
  19.  * @param string The search source string
  20.  * @param split  The splitter value
  21.  * @return An array of strings
  22.  * @author REghZy
  23.  */
  24. @Nonnull
  25. public static String[] split_old(@Nullable String string, @Nullable String split) {
  26.     int srcLen, tarLen, splitCount;
  27.     if (string == null || (srcLen = string.length()) == 0)
  28.         return EMPTY_STR_ARR;
  29.     if (split == null || (tarLen = split.length()) == 0)
  30.         return charToStringArray(string.toCharArray());
  31.    
  32.     if ((splitCount = countOccurrences(string, split)) == 0)
  33.         return new String[]{string};
  34.    
  35.     String[] array = new String[splitCount + 1];
  36.     char first = split.charAt(0);
  37.     int k = 0, last = 0;
  38.     for(int i = 0; i < string.length(); i++) {
  39.         if (string.charAt(i) != first)
  40.             continue;
  41.         Boolean match = matchAt(string, srcLen, split, tarLen, i);
  42.         if (match == null) {
  43.             break;
  44.         }
  45.         else if (match) {
  46.             array[k++] = string.substring(last, i);
  47.             last = i + tarLen;
  48.             i += (tarLen - 1);
  49.         }
  50.     }
  51.     if (last <= srcLen)
  52.         array[k] = string.substring(last);
  53.     return array;
  54. }
  55.  
  56. /**
  57.  * Searches the given string for all occurrences of the given value
  58.  * @param string The search source string
  59.  * @param value The search value
  60.  * @return The occurrence count of 'value' in 'string'
  61.  * @author REghZy
  62.  */
  63. public static int countOccurrences(String string, String value) {
  64.     int count = 0, len = value.length(), index = -len;
  65.     while ((index = string.indexOf(value, index + len)) != -1)
  66.         ++count;
  67.     return count;
  68. }
  69.  
  70. /**
  71.  * Splits the given string into an array, split by the given splitter string
  72.  * @param string The search source string
  73.  * @param split  The splitter value
  74.  * @return An array of strings
  75.  * @author REghZy
  76.  */
  77. @Nonnull
  78. public static String[] split(@Nullable String string, @Nullable String split) {
  79.     int srcLen, tarLen, splitCount;
  80.     if (string == null || (srcLen = string.length()) == 0)
  81.         return EMPTY_STR_ARR;
  82.     if (split == null || (tarLen = split.length()) == 0)
  83.         return charToStringArray(string.toCharArray());
  84.    
  85.     if ((splitCount = countOccurrences(string, split)) == 0)
  86.         return new String[]{string};
  87.    
  88.     String[] array = new String[splitCount + 1];
  89.     char first = split.charAt(0);
  90.     int i = -tarLen, last = 0, k = 0;
  91.     while ((i = string.indexOf(split, i + tarLen)) != -1) {
  92.         array[k++] = string.substring(last, i);
  93.         last = i + tarLen;
  94.     }
  95.     if (last <= srcLen)
  96.         array[k] = string.substring(last);
  97.     return array;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement