当前位置: 技术问答>java相关
如何用java实现字符串replace(str1,str2)
来源: 互联网 发布时间:2015-09-03
本文导语: ;) | 在jdk1。3里面没有相应的api提供,只能自己写。在jdk1。4里面有。 方法如下: public String stringReplace(String sourceString, String toReplaceString, String replaceString) { String returnString = sourceString; ...
;)
|
在jdk1。3里面没有相应的api提供,只能自己写。在jdk1。4里面有。
方法如下:
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()? 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 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()? 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;
}
|
简单啊,
java.net.URLDecoder;
java.net.URLEncoder;
java.net.URLDecoder;
java.net.URLEncoder;