public String stringReplace(String sourceString, String toReplaceString, String replaceString) { String returnString = sourceString; int stringLength = 0; if(toReplaceString != null) { stringLength = toReplaceString.length(); } if(returnString != null && returnString.length() > stringLength) { int max = 0; String S4 = ""; for(int i = 0; i < sourceString.length(); i++) { max = i + toReplaceString.length() > sourceString.length()? sourceString.length():i + stringLength; String S3 = sourceString.substring(i, max); if(!S3.equals(toReplaceString)) { S4 += S3.substring(0,1); } else { S4 += replaceString; i += stringLength -1 ; } } returnString = S4; } return returnString; }
给你个类,它以#1,#2为替换标志,方法都是static的:import java.io.*; public class StringReplacer{ static String s = "Congratulations, you have successfully booked air tickets with #1 through #2."; public StringReplacer(){ } /* * Specify the substring to be replaced and also the new substring to put into the original String. * @param s the original String * code1 substring in the original String to be replaced * replacement1 String to take place of code1 * code2 another substring to be replaced * replacement2 String to take place to code2 */ public static String replace(String s, String code, String replacement){ int startInt = 0, endInt = 0; startInt = s.indexOf(code); if (startInt == -1){return s;} endInt = startInt + code.length(); s = s.substring(0, startInt) + replacement + s.substring(endInt, s.length()); return s; } /* * Specify the substring to be replaced and also the new substring to put into the original String. * @param s the original String * code1 substring in the original String to be replaced * replacement1 String to take place of code1 * code2 another substring to be replaced * replacement2 String to take place to code2 */ public static String replace(String s, String code1, String replacement1, String code2, String replacement2){ s = replace(s, code1, replacement1); s = replace(s, code2, replacement2); return s; } public static void main(String args[]){ System.out.println(StringReplacer.replace(s, "#1", "Airline Company A", "#2", "Airline Agency B")); System.out.println(s); } }
{
String returnString = sourceString;
int stringLength = 0;
if(toReplaceString != null)
{
stringLength = toReplaceString.length();
}
if(returnString != null && returnString.length() > stringLength)
{
int max = 0;
String S4 = "";
for(int i = 0; i < sourceString.length(); i++)
{
max = i + toReplaceString.length() > sourceString.length()? sourceString.length():i + stringLength;
String S3 = sourceString.substring(i, max);
if(!S3.equals(toReplaceString))
{
S4 += S3.substring(0,1);
}
else
{
S4 += replaceString;
i += stringLength -1 ;
}
}
returnString = S4;
}
return returnString;
}
public class StringReplacer{
static String s = "Congratulations, you have successfully booked air tickets with #1 through #2.";
public StringReplacer(){
} /*
* Specify the substring to be replaced and also the new substring to put into the original String.
* @param s the original String
* code1 substring in the original String to be replaced
* replacement1 String to take place of code1
* code2 another substring to be replaced
* replacement2 String to take place to code2
*/
public static String replace(String s, String code, String replacement){
int startInt = 0, endInt = 0; startInt = s.indexOf(code); if (startInt == -1){return s;} endInt = startInt + code.length(); s = s.substring(0, startInt) + replacement + s.substring(endInt, s.length()); return s;
} /*
* Specify the substring to be replaced and also the new substring to put into the original String.
* @param s the original String
* code1 substring in the original String to be replaced
* replacement1 String to take place of code1
* code2 another substring to be replaced
* replacement2 String to take place to code2
*/
public static String replace(String s, String code1, String replacement1, String code2, String replacement2){
s = replace(s, code1, replacement1);
s = replace(s, code2, replacement2);
return s;
}
public static void main(String args[]){
System.out.println(StringReplacer.replace(s, "#1", "Airline Company A", "#2", "Airline Agency B"));
System.out.println(s);
}
}