Java Data Type How to - Insert special characters into a string








Question

We would like to know how to insert special characters into a string.

Answer

import java.util.StringTokenizer;
// w w  w . j  a  v a2s  .c om
public class Main {
  public static void main(String[] args) {
    String temp = "<NOUN>Nitin<NOUN> <NOUN>test<NOUN>";
    String finalString = "this is a test";

    StringTokenizer x = new StringTokenizer(temp, " ");
    while (x.hasMoreTokens()) {

      String token = x.nextToken();

      String findword = token.replaceAll("<NOUN>", "");
      String findword1 = findword.replaceAll("</NOUN>", "");

      String modifiedString = finalString.replaceFirst(findword1, "<NOUN>"
          + findword1 + "</NOUN>");
      finalString = modifiedString;
    }
    System.out.println(finalString);
  }
}