title: java.lang.OutOfMemoryError on String.replace
body: <p>I have one text file which contains 234 lines of string1@string2@string3.</p>
  
  <pre><code>kya@きゃ@キャ
  kyu@きゅ@キュ
  kyo@きょ@キョ
  sha@しゃ@シャ
  shu@しゅ@シュ
  ...so 234 lines
  </code></pre>
  
  <p>I am writing converter which converts word with string2 or string3 to the word with string1.</p>
  
  <pre><code>InputStream is = context.getResources().openRawResource(R.raw.kanatoromaji);
  InputStreamReader isr = new InputStreamReader(is);
  BufferedReader reader = new BufferedReader(isr);
  String line;
  try {
      while ((line = reader.readLine()) != null) {
          String[] parts = line.split("@");
          String romaji = parts[0];
          String hiragana=parts[1];
          String katakana = parts[2];
  
          if (hiragana!=null&amp;&amp;word.contains(hiragana)) {
                  word = word.replace(hiragana, romaji);//in this line getting outOfMemory error
          }
          if (word.contains(katakana)) {
              word = word.replace(katakana, romaji);
          }
      }
  
  } catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>I am calling this method many times (200k-300k times in one run). So this causing this error:
  07-24 00:52:32.859  10848-10848/net.joerichard.test E/AndroidRuntime﹕ FATAL </p>
  
  <pre><code>EXCEPTION: main
      java.lang.OutOfMemoryError
              at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
              at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:132)
              at java.lang.StringBuilder.append(StringBuilder.java:124)
              at java.lang.String.replace(String.java:1367)
              at net.joerichard.test.Converter.kanaToRomaji(Converter.java:32)
              at net.joerichard.test.MainActivity.migrateKanaKanji(MainActivity.java:222)
              at net.joerichard.test.MainActivity.access$700(MainActivity.java:29)
              at net.joerichard.test.MainActivity$10.onClick(MainActivity.java:131)
              at android.view.View.performClick(View.java:4240)
              at android.view.View$PerformClick.run(View.java:17721)
              at android.os.Handler.handleCallback(Handler.java:730)
              at android.os.Handler.dispatchMessage(Handler.java:92)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:5103)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:525)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>What is problem in my code? How to solve it?</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>According to JFPicard's answer, I converted my String word to StringBuilder sbWord. Then I tried to replace string2 and string3 using StringBuilder. I never replaced StringBuilder before. I search in Google and found this <a href="https://stackoverflow.com/a/3472705/2255924">solution</a> to replace my StringBuilder parts. Now my code looks like this:</p>
  
  <pre><code>public static String kanaToRomaji(Context context, String word) {
  
      String kana = word;
      StringBuilder sbWord = new StringBuilder(word);
  
      InputStream is = context.getResources().openRawResource(R.raw.kanatoromaji);
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader reader = new BufferedReader(isr);
      String line;
      try {
          while ((line = reader.readLine()) != null) {
              String[] parts = line.split("@");
              String romaji = parts[0];
              String hiragana=parts[1];
              String katakana = parts[2];
  
              if (hiragana!=null&amp;&amp;word.contains(hiragana)) {
                  sbWord = replaceAll(sbWord, hiragana, romaji);//in this line getting outOfMemory error
              }
              if (word.contains(katakana)) {
                  sbWord = replaceAll(sbWord, katakana, romaji);
              }
          }
  
      }
      catch (IOException e) {
          e.printStackTrace();
      }
  
      char[] chars = kana.toCharArray();
      for (char character: chars)
      {
          if(sbWord.toString().contains(String.valueOf(character)))
          {
              new MyLog(kana+":"+sbWord.toString());
              break;
          }
      }
  
      return word;
  }
  </code></pre>
  
  <p>Method where I replace my StringBuilder parts:</p>
  
  <pre><code>public static StringBuilder replaceAll(StringBuilder builder, String from, String to)
  {
      int index = builder.indexOf(from);
      while (index != -1)
      {
          builder.replace(index, index + from.length(), to);
          index += to.length(); // Move to the end of the replacement
          index = builder.indexOf(from, index);
      }
      return builder;
  }
  </code></pre>
  
  <p>Now I am getting this error:
  07-24 01:23:52.890  13512-13512/net.joerichard.test E/AndroidRuntime﹕ FATAL </p>
  
  <blockquote>
    <p>EXCEPTION: main
        java.lang.OutOfMemoryError
                at java.lang.AbstractStringBuilder.move(AbstractStringBuilder.java:397)
                at java.lang.AbstractStringBuilder.insert0(AbstractStringBuilder.java:356)
                at java.lang.AbstractStringBuilder.replace0(AbstractStringBuilder.java:442)
                at java.lang.StringBuilder.replace(StringBuilder.java:637)
                at net.joerichard.test.Converter.replaceAll(Converter.java:65)
                at net.joerichard.test.Converter.kanaToRomaji(Converter.java:33)
                at net.joerichard.test.MainActivity.migrateKanaKanji(MainActivity.java:222)
                at net.joerichard.test.MainActivity.access$700(MainActivity.java:29)
                at net.joerichard.test.MainActivity$10.onClick(MainActivity.java:131)
                at android.view.View.performClick(View.java:4240)
                at android.view.View$PerformClick.run(View.java:17721)
                at android.os.Handler.handleCallback(Handler.java:730)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:5103)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:525)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                at dalvik.system.NativeStart.main(Native Method)</p>
  </blockquote>
  
  <p><strong>What to do in this situation?</strong></p>