当前位置: 技术问答>java相关
字符替换,更深问题讨论有分。
来源: 互联网 发布时间:2015-08-30
本文导语: 从JTextPane用getText()获得String,如何将回车替换成别的字符? | 把String转换为StringBuffer更好 | jdk1。4里面提供字符串替换函数,如果你用的是jdk1。3 那就用下面的函数替换吧。我也是...
从JTextPane用getText()获得String,如何将回车替换成别的字符?
|
把String转换为StringBuffer更好
|
jdk1。4里面提供字符串替换函数,如果你用的是jdk1。3 那就用下面的函数替换吧。我也是收藏的:
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;
}
|
rn
|
用indexOf("n")查出回车符的位置,然后用replace()方法替换。
|
兄弟:控制臺copy的是自動會換行的.
|
回车是“n”,字符串替换你应该会写吧