当前位置: 技术问答>java相关
紧急!保证给你的100分,有谁能提供替换字符串的代码并举出正确的例子,
来源: 互联网 发布时间:2015-05-19
本文导语: 有谁能提供替换字符串的代码并举出正确的例子 有几个人提供了但是编译的时候总出现下列错误: 提示错误如下: javax.servlet.ServletException: Compilation error occured: Found 4 errors in JSP file: replace.jsp:2: Syntax: ; exp...
有谁能提供替换字符串的代码并举出正确的例子
有几个人提供了但是编译的时候总出现下列错误:
提示错误如下:
javax.servlet.ServletException: Compilation error occured:
Found 4 errors in JSP file:
replace.jsp:2: Syntax: ; expected instead of this token
replace.jsp:2: Syntax: ; expected instead of this token
replace.jsp:2: Syntax: ; expected instead of this token
replace.jsp:2: Syntax: ; expected instead of this token
这是为什么?
有几个人提供了但是编译的时候总出现下列错误:
提示错误如下:
javax.servlet.ServletException: Compilation error occured:
Found 4 errors in JSP file:
replace.jsp:2: Syntax: ; expected instead of this token
replace.jsp:2: Syntax: ; expected instead of this token
replace.jsp:2: Syntax: ; expected instead of this token
replace.jsp:2: Syntax: ; expected instead of this token
这是为什么?
|
replace.jsp
=0)
{
newString+=OldString.substring(0,index);//取匹配符的前半部份.
OldString=OldString.substring(index+oldStr.length());// 取匹配符的后半部份
newString+=newStr; //加上新符.
}
newString+=OldString; //加入后半份.
return newString;
}
|
看看这个
/**
* 替代linde中的oldString为newString
*
* @参数 line 需要做替代的字符串
* @参数 oldString the String that should be replaced by newString
* @param newString the String that will replace all instances of oldString
*
* @return a String will all instances of oldString replaced by newString
*/
public static final String replace( String line, String oldString, String newString )
{
if (line == null) {
return null;
}
int i=0;
if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* 替代linde中的oldString为newString
*
* @参数 line 需要做替代的字符串
* @参数 oldString the String that should be replaced by newString
* @param newString the String that will replace all instances of oldString
*
* @return a String will all instances of oldString replaced by newString
*/
public static final String replace( String line, String oldString, String newString )
{
if (line == null) {
return null;
}
int i=0;
if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) {
char [] line2 = line.toCharArray();
char [] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while( ( i=line.indexOf( oldString, i ) ) > 0 ) {
buf.append(line2, j, i-j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
|
我写的函数,希望有用
function LDReplaceSubString( sourceList , fromList , toList )
{
var i ;
var iLength ;
var strTmp ;
var iTmpLenth ;
iLength = fromList.length ;
strTmp = sourceList ;
while( strTmp.indexOf(fromList) >= 0 )
{
i = strTmp.indexOf(fromList) ;
iTmpLenth = strTmp.length ;
if ( i==0 )
{strTmp = toList + strTmp.substring(i+iLength,iTmpLenth) ;}
else
{strTmp = strTmp.substring(0,i-1) + toList + strTmp.substring(i+iLength,iTmpLenth) ;}
}
return( strTmp ) ;
}
function LDReplaceSubString( sourceList , fromList , toList )
{
var i ;
var iLength ;
var strTmp ;
var iTmpLenth ;
iLength = fromList.length ;
strTmp = sourceList ;
while( strTmp.indexOf(fromList) >= 0 )
{
i = strTmp.indexOf(fromList) ;
iTmpLenth = strTmp.length ;
if ( i==0 )
{strTmp = toList + strTmp.substring(i+iLength,iTmpLenth) ;}
else
{strTmp = strTmp.substring(0,i-1) + toList + strTmp.substring(i+iLength,iTmpLenth) ;}
}
return( strTmp ) ;
}
|
int i;
String str="mynameishu";//原始字符串
String rstr="name";//要替换掉的
String rstrit="brother";//用来替换的
for (i=0;i