이 기사는 문자열에 지정된 서브 스트링이 포함되어 있는지 여부를 결정하기위한 JavaScript 방법을 설명합니다. 참조를 위해 공유하십시오. 특정 분석은 다음과 같습니다.
다음 JS 코드는 문자열 객체에 대한 포함 메소드를 정의하여 문자열에 서브 스트링이 포함되어 있는지 결정합니다. 이는 매우 유용합니다.
if (! array.prototype.indexof) {array.prototyp.indexof = function (obj, start) {for (var i = (start || 0), j = this.length; i <j; i ++) {if (this [i] === obj) {return i; }} 반환 -1; }} if (! string.prototype.contains) {string.prototype.contains = function (arg) {return !! ~ this.indexof (arg); };}아래는 브라우저에서 실행할 수있는 자세한 사용 예제입니다.
다음과 같이 코드를 복사하십시오. 두 줄을 입력하고 Strign 1이 문자열 2가 포함되어 있는지 확인하십시오. <br> <br>
문자열 1 : <입력 id = "foo"type = "text"value = "빠른 갈색 여우가 점프"> <br>
문자열 2 : <입력 id = "bar"type = "text"value = "Fox Jumps"> <br> <br>
<버튼 onclick = "checkstring ()"> 문자열 1이 문자열 2 </button>을 포함하는지 확인하려면 클릭하십시오.
<cript>
if (! array.prototype.indexof) {
Array.prototype.indexof = function (obj, start) {
for (var i = (시작 || 0), j = this.length; i <j; i ++) {
if (this [i] === obj) {return i; }
}
반품 -1;
}
}
if (! string.prototype.contains) {
String.prototype.contains = function (arg) {
반환 !! ~ this.indexof (arg);
};
}
함수 checkstring () {
var foo = document.getElementById ( "foo"). 값;
var bar = document.getElementById ( "bar"). 값;
경고 (foo.contains (bar));
}
</스크립트>
이 기사가 모든 사람의 JavaScript 프로그래밍에 도움이되기를 바랍니다.