반응형
치환함수에 대해서 알아봅니다.
JavaScript
먼저 자바스크립트에서 문자열 치환방법입니다.
<script language="JavaScript">
str = "대니를 바꿔버립시다."
re = "대니"
newstr = str.replace(re, "전성대");
document.write(newstr)
</script>
출력결과 : 전성대를 바꿔버립시다.
ASP
이번엔 ASP에서 치환을 해봅시다.
<%
str = "대니를 바꿔버립시다."
re = "대니"
newstr = replace(str,re,"전성대")
response.write newstr
%>
출력결과 : 전성대를 바꿔버립시다.
PHP
이번엔 php에서의 문자열 치환입니다.
<?
$str = "대니를 바꿔버립시다.";
$re = "대니";
$newstr = str_replace("대니","전성대",$str)
echo ($newstr);
?>
출력결과 : 전성대를 바꿔버립시다.
JSP
이번엔 jsp에서의 문자열 치환입니다.
<%
String usf_replace(String src, String oldstr, String newstr) {
if (src == null) return null;
StringBuffer dest = new StringBuffer("");
try {
int len = oldstr.length();
int srclen = src.length();
int pos = 0;
int oldpos = 0;
while ((pos = src.indexOf(oldstr, oldpos)) >= 0) {
dest.append(src.substring(oldpos, pos));
dest.append(newstr);
oldpos = pos + len;
}
if (oldpos < srclen)
dest.append(src.substring(oldpos, srclen));
} catch ( Exception e ) {
e.printStackTrace();
}
return dest.toString();
}
결과 = usf_replace( "대니를 바꿔버립시다.", "대니", "전성대");
%>
출력결과 : 전성대를 바꿔버립시다.
JavaScript
먼저 자바스크립트에서 문자열 치환방법입니다.
<script language="JavaScript">
str = "대니를 바꿔버립시다."
re = "대니"
newstr = str.replace(re, "전성대");
document.write(newstr)
</script>
출력결과 : 전성대를 바꿔버립시다.
ASP
이번엔 ASP에서 치환을 해봅시다.
<%
str = "대니를 바꿔버립시다."
re = "대니"
newstr = replace(str,re,"전성대")
response.write newstr
%>
출력결과 : 전성대를 바꿔버립시다.
PHP
이번엔 php에서의 문자열 치환입니다.
<?
$str = "대니를 바꿔버립시다.";
$re = "대니";
$newstr = str_replace("대니","전성대",$str)
echo ($newstr);
?>
출력결과 : 전성대를 바꿔버립시다.
JSP
이번엔 jsp에서의 문자열 치환입니다.
<%
String usf_replace(String src, String oldstr, String newstr) {
if (src == null) return null;
StringBuffer dest = new StringBuffer("");
try {
int len = oldstr.length();
int srclen = src.length();
int pos = 0;
int oldpos = 0;
while ((pos = src.indexOf(oldstr, oldpos)) >= 0) {
dest.append(src.substring(oldpos, pos));
dest.append(newstr);
oldpos = pos + len;
}
if (oldpos < srclen)
dest.append(src.substring(oldpos, srclen));
} catch ( Exception e ) {
e.printStackTrace();
}
return dest.toString();
}
결과 = usf_replace( "대니를 바꿔버립시다.", "대니", "전성대");
%>
출력결과 : 전성대를 바꿔버립시다.