Baseline Widely available
charAt() í¨ìë 문ìì´ìì í¹ì ì¸ë±ì¤ì ìì¹íë ì ëì½ë ë¨ì¼ë¬¸ì를 ë°íí©ëë¤.
ìëí´ ë³´ê¸°const sentence = "The quick brown fox jumps over the lazy dog.";
const index = 4;
console.log(`The character at index ${index} is ${sentence.charAt(index)}`);
// Expected output: "The character at index 4 is q"
구문 매ê°ë³ì
index
문ìì´ ë´ì 문ìë ì¼ìª½ìì ì¤ë¥¸ìª½ì¼ë¡ ìë²(ì¸ë±ì¤)ì´ ë§¤ê²¨ì§ëë¤. 첫 ë²ì§¸ 문ìì ìë²ì 0, ê·¸ë¦¬ê³ stringName
ì´ë¼ë ì´ë¦ì ê°ì§ 문ìì´ì ë§ì§ë§ 문ì ìë²ì stringName.length - 1
ì
ëë¤. index
ê° ë¬¸ìì´ ê¸¸ì´ë¥¼ ë²ì´ëë©´ ë¹ ë¬¸ìì´ì ë°ííê² ë©ëë¤.
index
를 ì ê³µíì§ ìì¼ë©´ 기본ê°ì 0ì
ëë¤.
ìë ìì ë 문ìì´ "Brave new world"
ì ë¤ë¥¸ ìì¹ì ìë 문ìë¤ì ì¶ë ¥í©ëë¤.
var anyString = "Brave new world";
console.log("The character at index 0 is '" + anyString.charAt() + "'");
// No index was provided, used 0 as default
console.log("The character at index 0 is '" + anyString.charAt(0) + "'");
console.log("The character at index 1 is '" + anyString.charAt(1) + "'");
console.log("The character at index 2 is '" + anyString.charAt(2) + "'");
console.log("The character at index 3 is '" + anyString.charAt(3) + "'");
console.log("The character at index 4 is '" + anyString.charAt(4) + "'");
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
íë¡ê·¸ë¨ì ì¤í ê²°ê³¼ë ìëì ê°ìµëë¤.
The character at index 0 is 'B'
The character at index 1 is 'r'
The character at index 2 is 'a'
The character at index 3 is 'v'
The character at index 4 is 'e'
The character at index 999 is ''
문ìì´ ë´ì 모ë 문ì ì»ê¸°
ìë ìì ë 문ìì´ ì 체를 ìííë©° ê° ë¬¸ìê° ìì íì§ íì¸íë íë¡ê·¸ë¨ì ëë¤. ì¬ì§ì´ 기본 ë¤êµì´ íë©´(Basic Multilingual Plane)ì í¬í¨ëì§ ìì 문ìë¤ì´ í¬í¨ëì´ ìë¤ê³ íëë¼ë ì ëìí©ëë¤.
var str = "A \uD87E\uDC04 Z"; // 기본 ë¤êµì´ íë©´ì í¬í¨ëì§ ìë 문ì를 ì¬ì©í©ëë¤.
for (var i = 0, chr; i < str.length; i++) {
if ((chr = getWholeChar(str, i)) === false) {
continue;
}
// Adapt this line at the top of each loop, passing in the whole string and
// the current iteration and returning a variable to represent the
// individual character
console.log(chr);
}
function getWholeChar(str, i) {
var code = str.charCodeAt(i);
if (Number.isNaN(code)) {
return ""; // Position not found
}
if (code < 0xd800 || code > 0xdfff) {
return str.charAt(i);
}
// High surrogate (could change last hex to 0xDB7F to treat high private
// surrogates as single characters)
if (0xd800 <= code && code <= 0xdbff) {
if (str.length <= i + 1) {
throw "High surrogate without following low surrogate";
}
var next = str.charCodeAt(i + 1);
if (0xdc00 > next || next > 0xdfff) {
throw "High surrogate without following low surrogate";
}
return str.charAt(i) + str.charAt(i + 1);
}
// Low surrogate (0xDC00 <= code && code <= 0xDFFF)
if (i === 0) {
throw "Low surrogate without preceding high surrogate";
}
var prev = str.charCodeAt(i - 1);
// (could change last hex to 0xDB7F to treat high private
// surrogates as single characters)
if (0xd800 > prev || prev > 0xdbff) {
throw "Low surrogate without preceding high surrogate";
}
// We can pass over low surrogates now as the second component
// in a pair which we have already processed
return false;
}
ë¹êµ¬ì¡°í í ë¹ì íì©íë ECMAScript 2016 íê²½ììë ìë ìì ì½ëê° ë ê°ê²°íê³ , 문ìê° ìë¬ê²ì´í¸ íì´ê° ëë ê²ì íì©í ëë ì¦ê°í´ì¼ íë ë³ì를 ìëì ì¼ë¡ ì¦ê°í기ì ìì ì½ëë³´ë¤ ë¤ì ë ì ì°í©ëë¤.
var str = "A\uD87E\uDC04Z"; // We could also use a non-BMP character directly
for (var i = 0, chr; i < str.length; i++) {
[chr, i] = getWholeCharAndI(str, i);
// Adapt this line at the top of each loop, passing in the whole string and
// the current iteration and returning an array with the individual character
// and 'i' value (only changed if a surrogate pair)
console.log(chr);
}
function getWholeCharAndI(str, i) {
var code = str.charCodeAt(i);
if (Number.isNaN(code)) {
return ""; // Position not found
}
if (code < 0xd800 || code > 0xdfff) {
return [str.charAt(i), i]; // Normal character, keeping 'i' the same
}
// High surrogate (could change last hex to 0xDB7F to treat high private
// surrogates as single characters)
if (0xd800 <= code && code <= 0xdbff) {
if (str.length <= i + 1) {
throw "High surrogate without following low surrogate";
}
var next = str.charCodeAt(i + 1);
if (0xdc00 > next || next > 0xdfff) {
throw "High surrogate without following low surrogate";
}
return [str.charAt(i) + str.charAt(i + 1), i + 1];
}
// Low surrogate (0xDC00 <= code && code <= 0xDFFF)
if (i === 0) {
throw "Low surrogate without preceding high surrogate";
}
var prev = str.charCodeAt(i - 1);
// (could change last hex to 0xDB7F to treat high private surrogates
// as single characters)
if (0xd800 > prev || prev > 0xdbff) {
throw "Low surrogate without preceding high surrogate";
}
// Return the next character instead (and increment)
return [str.charAt(i + 1), i + 1];
}
기본ë¤êµì´íë©´(Basic-Multilingual-Plane)ì´ ìë 문ìë¤ì ì§ìíëë¡ charAt()
ìì í기
ì´ë í non-BMP 문ìë¤ì´ ëíë¬ëì§ í¸ì¶ìê° ì íìê° ì기 ë문ì non-BMP 문ìë¤ì ì§ìíëë¡ íëë°ë ìì ìì ë¤ì´ ë ì주 ì¬ì©ëì§ë§, ì¸ë±ì¤ë¡ 문ì를 ì ííëë° ìì´ì 문ìì´ ë´ì ìë¡ê²ì´í¸ íì´ë¤ì´ íëì 문ìë¤ë¡ ì²ë¦¬ë길 ìíë¤ë©´, ìë ìì ì½ë를 ì¬ì©íë©´ ë©ëë¤.
function fixedCharAt(str, idx) {
var ret = "";
str += "";
var end = str.length;
var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
while (surrogatePairs.exec(str) != null) {
var li = surrogatePairs.lastIndex;
if (li - 2 < idx) {
idx++;
} else {
break;
}
}
if (idx >= end || idx < 0) {
return "";
}
ret += str.charAt(idx);
if (
/[\uD800-\uDBFF]/.test(ret) &&
/[\uDC00-\uDFFF]/.test(str.charAt(idx + 1))
) {
// Go one further, since one of the "characters" is part of a surrogate pair
ret += str.charAt(idx + 1);
}
return ret;
}
ëª
ì¸ ë¸ë¼ì°ì í¸íì± ê´ë ¨ë¬¸ì
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4