Replace a space in a string with another string
The function body is as follows
1 ////Replace spaces2 Private Static Char[] Replacetrim (Char[] str1,Char[] tidai) {3 //TODO Auto-generated method stubs4 intCount=0;5 for(inti = 0; i < str1.length; i++) {6 if(str1[i]== ") {7count++;8 }9 }Ten ///The length of the substituted symbol-the length of the space is the new to the length One intsize=str1.length+count* (tidai.length-1); A Char[] newstring=New Char[size]; -size--; - for(inti = str1.length-1; I >=0; i--) { the if(str1[i]== ") { - for(intj = 0; J < Tidai.length; J + +) { -Newstring[size-j]=tidai[tidai.length-j-1]; - } +size-=tidai.length; -}Else { +newstring[size]=Str1[i]; Asize--; at } - } - returnnewstring; -}
Testing the main function
public static void Main (string[] args) {String str1= "ac b CB"; String str2= "a B"; System.out.println (Replacetrim (Str1.tochararray (), "%". ToCharArray ())); System.out.println (Replacetrim (Str2.tochararray (), "%5". ToCharArray ()));}
Output:
ac%%b%cb%
a%5b
Java string substitution Repalce method implementation