with String, you can only replace one char with another char:
String replace(char oldChar, char newChar) with StringBuffer, you have
StringBuffer replace(int start, int end, String str) guess you might have to write you own, like:// Replace all occurrences of o in str with n,
// or only the first occurrence if all is false.
// replace("aaaa", "aa", "bbb", false) returns "bbbaa"
// replace("aaaa", "aa", "bbb", true) returns "bbbbbb"
String replace(String str, String o, String n, boolean all) {
if (str == null || o == null || o.length() == 0 || n == null)
throw new IllegalArgumentException("null or empty String");
StringBuffer result = null;
int oldpos = 0;
do {
int pos = str.indexOf(o, oldpos);
if (pos < 0)
break;
if (result == null)
result = new StringBuffer();
result.append(str.substring(oldpos, pos));
result.append(n);
pos += o.length();
oldpos = pos;
} while (all);
if (oldpos == 0) {
return str;
} else {
result.append(str.substring(oldpos));
return new String(result);
}
}
String replace(char oldChar, char newChar) with StringBuffer, you have
StringBuffer replace(int start, int end, String str) guess you might have to write you own, like:// Replace all occurrences of o in str with n,
// or only the first occurrence if all is false.
// replace("aaaa", "aa", "bbb", false) returns "bbbaa"
// replace("aaaa", "aa", "bbb", true) returns "bbbbbb"
String replace(String str, String o, String n, boolean all) {
if (str == null || o == null || o.length() == 0 || n == null)
throw new IllegalArgumentException("null or empty String");
StringBuffer result = null;
int oldpos = 0;
do {
int pos = str.indexOf(o, oldpos);
if (pos < 0)
break;
if (result == null)
result = new StringBuffer();
result.append(str.substring(oldpos, pos));
result.append(n);
pos += o.length();
oldpos = pos;
} while (all);
if (oldpos == 0) {
return str;
} else {
result.append(str.substring(oldpos));
return new String(result);
}
}
解决方案 »
- 超市管理系统
- 找一套BBS论坛的页面模板
- JSP如何在页面跳转时自动刷新session里的参数
- 马上给分: jsp 里面怎么处理才能同时显示中日英的字符??
- 项目管理人员和想做项目管理的人,快出招!!!!
- 一个半年都没有人回答的问题!在这期间我郁闷无限
- 找不到类啊!郁闷,欢迎wangdd(coffeeilove)进来
- jsp 中的PreparedStatement对象应用问题?
- JSP、 PHP、 .NET谁主沉浮?
- mysql配置问题?
- prepareStatement把我弄糊涂了,请帮帮我
- applet与servlet的通信,我想将APPLET中的参数传给SERVLET,总是通不过,详情请进
String scr="";
scr=replaceBeans.replace("aaaa","aa","bbb",true);
out.println(scr);
结果出现
org.apache.jasper.JasperException: Unable to compile class for JSPF:\MyJava\BsDather\bsdatagather\work\localhost_8080\_0002fTableTypes_0002ejspTableTypes_jsp_100.java:110: class bsdatagather.loginBean ?????? replace(java.lang.String, java.lang.String, java.lang.String, boolean)?
scr=loginBeanId.replace("aaaa","aa","bbb",true);
^
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.java.lang.String
有的