你的问题好简单,我给你个函数,可以将你的bb替换成任何东东
str=replacetag(str,"bb","2313231123!@!@@!#!@#$$%%$^\");public static String replacetag(String in_str,String in_tag,String repalace_text)
{
StringBuffer in_str_buffer=new StringBuffer(in_str);
int site;
site=in_str.indexOf(in_tag);
if (site!=-1) in_str_buffer=in_str_buffer.replace(site,site+in_tag.length(),repalace_text);
in_str=in_str_buffer.toString();
return in_str;
}
str=replacetag(str,"bb","2313231123!@!@@!#!@#$$%%$^\");public static String replacetag(String in_str,String in_tag,String repalace_text)
{
StringBuffer in_str_buffer=new StringBuffer(in_str);
int site;
site=in_str.indexOf(in_tag);
if (site!=-1) in_str_buffer=in_str_buffer.replace(site,site+in_tag.length(),repalace_text);
in_str=in_str_buffer.toString();
return in_str;
}
Replaces each substring of this string that matches the given regular expression with the given replacement. Str.replaceAll( "BB", "12" );
Replaces each substring of this string that matches the given regular expression with the given replacement.
replaceAll
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)Parameters:
regex - the regular expression to which this string is to be matched
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
NullPointerException - if regex is null
Since:
1.4
See Also:
Pattern
ublic String replaceAll(String regex,
String replacement)Replaces each substring of this string that matches the given regular expression with the given replacement.An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)
Parameters:
regex - the regular expression to which this string is to be matched
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
NullPointerException - if regex is null
Since:
1.4See Also:
Pattern不过要熟悉正则表达式
{
String returnString = sourceString;
ArrayList arrayList = new ArrayList();
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;
}
例如:
StringBuffer strBuf = new StringBuffer().append("AABBCCAABBCCAABBCC");
//例如要将第二个BB换成12
int index = -2;
for(int j = 0 ; j < 2 ; j++) //其中j<n,n即为要替换的第几个
index = strBuf.toString().indexOf("BB",index + 2);
strBuf.replace(index,index + 2,"12");
System.out.println(strBuf);