Posted on 2007. 9. 10. 10:15
Filed Under Java

Javascript
<script language="javascript">
str = "abcd";
restr = "ab";
newstr = str.replace(restr, "xx");
document.wrtie(newstr);
</script>
출력결과 : xxcd


JSP
<%
String strReplace(String str, String restr, String newstr) {
  if(str = null) return null;
  StringBuffer dest = new StringBuffer("");
  try {
    int len = restr.length();
    int strlen = str.length();
    int pos = 0;
    int repos = 0;

    while ((pos = str.indexOf(restr, repos)) >= 0) {
      dest.append(str.substring(repos, pos));
      dest.append(newstr);
      repos = pos + len;
    }
    if (repos < strlen)
      dest.appen(str.substring(repos, strlen));
  } catch (Exception e) {
    e.printStackTrace();
  }
  return dest.toString();
}
%>

strReplace("abcd", "ac", "xx");

출력결과 : xxcd



About

by 청지인

Notice

Counter

· Total
:
· Today
:
· Yesterday
: