java - How to remove multiple special characters from a String without any inbuilt functions like replaceAll() for jdk 1.4 below -
using below function able remove single special character, need multiple types of them removed single string.
static public string replaceall(string str, string replace, string replacement ) { stringbuffer sb = new stringbuffer( str ); int firstoccurrence = sb.tostring().indexof( replace ); while( firstoccurrence != -1 ) { sb.replace( firstoccurrence, firstoccurrence + replace.length(), replacement ); firstoccurrence = sb.tostring().indexof( replace ); } return sb.tostring(); }
needs works below jdk 1.4 without inbuilt function replaceall()
thanks.
what remove special characters, such control characters, non-ascii special characters ascii ones this.
public static string stripspecialcharacters(string str) { stringbuffer sb = new stringbuffer(); for(int i=0;i<str.length();i++) { char ch = str.charat(i); if (character.isletterordigit(ch) || character.isspacechar(ch)) sb.append(ch); } return sb.tostring(); }
note: want avoid creating multiple characters or re-arranging characters in stringbuffer these become o(n^2) operations in context.
Comments
Post a Comment