title: how to use java substring on Japanese utf-8 kanji
body: <p>Is it possible to use substring to extract single utf8 kanji from a string?  The problem is that utf-8 "characters" can have a length of 1, 2 or 3.</p>
  
  <p>For instance, length of "" is 6 so String.substring(1, 2) doesn't get the first complete character.</p>
  
  <p>In PERL, I could just use substr("", 1, 1) to get the first character, or substr("", 2, 1) to get the second character.</p>
  
  <p>UPDATE:
  Based on @msandiford's suggestion, I came up with this.</p>
  
  <pre><code>public class SplitKanji {
      private String [] splitKanji;
      private SplitKanji(String string) {
          int cpCount = string.codePointCount(0, string.length());
          splitKanji = new String[cpCount];
          int nextSlot = 0;
          for (int i = 0; i &lt; string.length();) {
              int ii = string.offsetByCodePoints(i, 1);
              splitKanji[nextSlot++] = string.substring(i, ii);
              i = ii;
          }
      }
      private String[] get() {
          return splitKanji;
      }
      public static void main(String[] args) {
          String startKanji = "私今日";
          SplitKanji myStuff = new SplitKanji(startKanji);
          String [] split = myStuff.get();
          System.out.print(startKanji + "=");
          for(String kanji: split)
              System.out.print(kanji + ":" + kanji.length() + ", ");
          System.out.println();
      }
  }
  </code></pre>