Note: The Stringbuilder,append,length method in Java makes it easy to solve string problems
1 /*2 * Sword means offer replacement space3 * xsf4 * */5 6 /*A function to begin replacing a space, length of the original array, lengths not calculated in Java/0*/7 classblankfun{8 Publicstring Replacebank (String str) {9 if(str==NULL)//Input safety judgmentTen return NULL; One //to build a new string AStringBuffer newstr =NewStringBuffer (); - for(intI=0;i<str.length (); i++){ - if(Str.charat (i) = = "){ theNewstr.append (' 0 '); -Newstr.append (' 2 '); -Newstr.append ('% '); - } + Else - Newstr.append (Str.charat (i)); + } A return NewString (NEWSTR); at } - } - - Public classReplaceblank { - Public Static voidMain (string[] args) { -String str = "x s F"; inBlankfun Blankfun =NewBlankfun (); - to System.out.println (Blankfun.replacebank (str)); + - } the}
Pro4 Replace space (Java)