자바스크립트 문자열 객체
문자열 개체 정의
JavaScript 문자열 개체는 텍스트 문자열을 처리하는 데 사용됩니다. String 객체를 생성하는 구문은 다음과 같습니다.
<script language="JavaScript"> var str_object = new String( str ); var str1 = String( str ); var str2 = str; </script>
위의 세 가지 방법 중 첫 번째 방법만 String 생성자를 사용하여 문자열 객체를 엄격하게 정의하고 객체를 반환합니다. 두 번째는 String 함수를 호출하여 매개변수 str을 원래 문자열로 변환하고 반환하는 것입니다. 세 번째 방법은 문자열 변수를 정의하는 것이지만 JavaScript에서는 여전히 문자열 개체로 처리됩니다.
다음 명령문을 실행하여 차이점을 알아보세요.
alert( typeof str_object ); // 출력 object
alert( typeof str1 ); // 출력 문자열
alert( typeof str2 ); 개체 속성
속성 설명 프로토타입 객체에 속성 및 메서드 추가
문자 문자열은 길이 속성 길이를 사용하여 계산합니다. 문자열의 길이:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var txt = "Hello World!";
document.write("<p>" + txt.length + "</p>");
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write("<p>" + txt.length + "</p>");
</script>
</body>
</html>String은 indexOf()를 사용하여 첫 번째 항목을 찾습니다. 문자열에서 지정된 문자: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="p1">Click the button to locate where "locate" first occurs.</p>
<p id="p2">0</p>
<button onclick="myFunction()">点击查看</button>
<script>
function myFunction(){
var str=document.getElementById("p1").innerHTML;
var n=str.indexOf("locate");
document.getElementById("p2").innerHTML=n+1;
}
</script>
</body>
</html>match() 함수는 문자를 찾는 데 사용됩니다. 문자열에서 특정 문자를 찾고, 발견되면 이 문자를 반환합니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));
</script>
</body>
</html>replace() 메서드는 문자열의 특정 문자를 다른 문자로 바꿉니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<button onclick="myFunction()">点我</button>
<p id="demo">请访问 Microsoft!</p>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("Microsoft","php.cn");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>문자열 대/소문자 변환에는 toUpperCase() / toLowerCase() 함수를 사용하세요.
var txt="Hello World!"; // Stringvar txt1=txt.toUpperCase(); 대문자로
var txt2=txt.toLowerCase(); // txt2 텍스트는 소문자로 변환됩니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="demo"></p>
<button onclick="myFunction()">点击显示</button>
<script>
function myFunction(){
var str="a,b,c,d,e,f";
var n=str.split(",");
document.getElementById("demo").innerHTML=n[2];
}
</script>
</body>
</html>특수 문자 Javascript( ) 아포스트로피, 따옴표, 기타 특수 기호 등 특수 기호를 삽입합니다. 다음 JavaScript 코드 보기:var txt="우리는 소위 북쪽에서 온 "바이킹"입니다.";document.write(txt); JavaScript에서는 작은따옴표나 큰따옴표를 사용하여 문자열을 시작하고 중지합니다. 이는 위의 문자열이 다음과 같이 잘려짐을 의미합니다. We are the 소위 위의 문제를 해결하려면 백슬래시를 사용하여 따옴표를 이스케이프 처리할 수 있습니다. var txt="We are the 소위 "Vikings " from the north."; JavaScript는 올바른 텍스트 문자열을 출력합니다. We are the 소위 "Vikings" from the north. 다음 표에는 기타 특수 문자, 특수 문자가 나열되어 있습니다. 백슬래시를 사용하여 이스케이프할 수 있습니다. Output " 큰따옴표
document.write(txt);
' 작은따옴표
- 코스 추천
- 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~ 















