From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../global_objects/string/lastindexof/index.html | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 files/ko/web/javascript/reference/global_objects/string/lastindexof/index.html (limited to 'files/ko/web/javascript/reference/global_objects/string/lastindexof') diff --git a/files/ko/web/javascript/reference/global_objects/string/lastindexof/index.html b/files/ko/web/javascript/reference/global_objects/string/lastindexof/index.html new file mode 100644 index 0000000000..d2244feee5 --- /dev/null +++ b/files/ko/web/javascript/reference/global_objects/string/lastindexof/index.html @@ -0,0 +1,105 @@ +--- +title: String.prototype.lastIndexOf() +slug: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf +tags: + - JavaScript + - Method + - Prototype + - Reference + - String +translation_of: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf +--- +
{{JSRef}}
+ +

lastIndexOf() 메서드는 주어진 값과 일치하는 부분을 fromIndex로부터 역순으로 탐색하여, 최초로 마주치는 인덱스를 반환합니다. 일치하는 부분을 찾을 수 없으면 -1을 반환합니다.

+ +
{{EmbedInteractiveExample("pages/js/string-lastindexof.html", "shorter")}}
+ + + +

구문

+ +
str.lastIndexOf(searchValue[, fromIndex])
+ +

매개변수

+ +
+
searchValue
+
탐색할 문자열. 빈 값을 제공할 경우 fromIndex를 반환합니다.
+
fromIndex {{optional_inline}}
+
탐색의 시작점으로 사용할 인덱스. 기본값은 +Infinity입니다. fromIndex >= str.length인 경우 모든 문자열을 탐색합니다. fromIndex < 0인 경우엔 0을 지정한 것과 동일합니다.
+
+ +

반환 값

+ +

문자열 내에서 searchValue가 마지막으로 등장하는 인덱스. 등장하지 않으면 -1.

+ +

설명

+ +

문자열의 문자는 왼쪽에서 오른쪽으로 인덱스를 매깁니다. 첫 번째 문자의 인덱스는 0이며, 마지막 문자의 인덱스는 str.length -1입니다.

+ +
'canal'.lastIndexOf('a');     //  3 반환
+'canal'.lastIndexOf('a', 2);  //  1 반환
+'canal'.lastIndexOf('a', 0);  // -1 반환
+'canal'.lastIndexOf('x');     // -1 반환
+'canal'.lastIndexOf('c', -5); //  0 반환
+'canal'.lastIndexOf('c', 0);  //  0 반환
+'canal'.lastIndexOf('');      //  5 반환
+'canal'.lastIndexOf('', 2);   //  2 반환
+
+ +
+

참고: 'abab'.lastIndexOf('ab', 2)는 0이 아니고 2를 반환합니다. fromIndex는 탐색의 시작점만 제한하기 때문입니다.

+
+ +

대소문자 구분

+ +

lastIndexOf() 메서드는 대소문자를 구분합니다. 예를 들어, 아래 예제는 -1을 반환합니다.

+ +
'Blue Whale, Killer Whale'.lastIndexOf('blue'); // -1 반환
+
+ +

예제

+ +

indexOf()와 lastIndexOf() 사용하기

+ +

아래 예제는 문자열 "Brave new world" 내에서 특정 값의 위치를 확인하기 위해 {{jsxref("String.prototype.indexOf()", "indexOf()")}}와 lastIndexOf()를 사용합니다.

+ +
let anyString = 'Brave new world';
+
+console.log('시작점으로부터 처음 만나는 w의 위치는 ' + anyString.indexOf('w'));
+// logs 8
+console.log('끝점으로부터 처음 만나는 w의 위치는 ' + anyString.lastIndexOf('w'));
+// logs 10
+console.log('시작점으로부터 처음 만나는 "new"의 위치는 ' + anyString.indexOf('new'));
+// logs 6
+console.log('끝점으로부터 처음 만나는 "new"의 위치는 ' + anyString.lastIndexOf('new'));
+// logs 6
+
+ +

명세

+ + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-string.prototype.lastindexof', 'String.prototype.lastIndexOf')}}
+ +

브라우저 호환성

+ +
{{Compat("javascript.builtins.String.lastIndexOf")}}
+ +

같이 보기

+ + -- cgit v1.2.3-54-g00ecf