Friday, May 4, 2012

How to replace a specific char in String in java


public String replace(String str, int index, char replace) {
      if (str == null) {
            return str;
      } else if (index < 0 && index >= str.length()) {
            return str;
      }
      char[] chars = str.toCharArray();
      chars[index] = replace;
      return String.valueOf(chars);
}

No comments:

Post a Comment