title: Java, writing my own split string method
body: <p>I need to be able to write my own split string method so that input like</p>
  
  <pre><code>String[] test1 = mySplit("ab#cd#efg#", "#");
  System.out.println(Arrays.toString(test1));
  </code></pre>
  
  <p>will print <code>[ab, #, cd, #, efg, #]</code> to the console.
  So far I've got it to split like that but my way leaves awkward spaces where 2 delimiters are in a row, or a delimiter is at the start of the input.</p>
  
  <pre><code>public static String[] mySplit(String str, String regex)
  {
      String[] storeSplit = new String[str.length()];
      char compare1, compare2;
      int counter = 0;
  
      //Initializes all the string[] values to "" so when the string
      //and char concatonates, 'null' doesn't appear.
      for(int i=0; i&lt;str.length(); i++) {
          storeSplit[i] = "";
      }
  
      //Puts the str values into the split array and concatonates until
      //a delimiter is found, then it moves to the next array index.
      for(int i=0; i&lt;str.length(); i++) {
          compare1 = str.charAt(i);
          compare2 = regex.charAt(0);
  
              if(!(compare1 == compare2)) {
                  storeSplit[counter] += ""+str.charAt(i);
              } else {
                  counter++;
                  storeSplit[counter] = ""+str.charAt(i);
                  counter++;
              }
      }
      return storeSplit;
  }
  </code></pre>
  
  <p>When I use that method in my Test main, I get the output [ab, #, cd, #, efg, #, , , , ]. So I'm lost on how to fix the spacing of it all and I'll also need to be able to allow multiple delimiters which my code currently doesn't handle.</p>
  
  <p>Also I know this code is really sloppy at the moment, just trying to lay down the concepts before the optimization.</p>