1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
---
title: String.prototype.charAt()
slug: Web/JavaScript/Reference/Global_Objects/String/charAt
translation_of: Web/JavaScript/Reference/Global_Objects/String/charAt
---
<div>{{JSRef}}</div>
<p><span class="seoSummary"><em>Method </em><strong><code>charAt()</code></strong> pada Objek {{jsxref("String")}} mengembalikan string baru yang berisi kode lokasi single UTF-16 pada offset yang ditentukan ke dalam string.</span></p>
<div>{{EmbedInteractiveExample("pages/js/string-charat.html", "shorter")}}</div>
<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox notranslate">let <var>character</var> = <var>str</var>.charAt(<var>index</var>)</pre>
<h3 id="Parameter">Parameter</h3>
<dl>
<dt><code><var>index</var></code></dt>
<dd>Sebuah integer antara <code>0</code> sampai <code><var>str</var>.length - 1</code>. Bila nilai <code><var>index</var></code> bukan dalam bentuk integer atau <code><var>index</var></code> tidak ditentukan, maka nilai <code><var>index</var></code> akan bernilai <code>0</code>, sehingga karakter pertama varibel <code><var>str</var></code> akan dikembalikan/ di-<em>return</em>.</dd>
</dl>
<h3 id="Nilai_Return">Nilai <em>Return</em></h3>
<p>Sebuah string yang merepresentasikan sebuah karakter pada indeks tertentu. Jika <code><var>index</var></code> indeks berada di luar <em>range</em>, maka <code>charAt()</code> akan me-<em>return </em>string kosong.</p>
<h2 id="Deskripsi">Deskripsi</h2>
<p>Karakter dalam sebuah string diberi indeks dari kiri ke kanan. Indeks karakter pertama adalah 0, dan index karakter terakhir dalam string — misal nama stringnya <code><var>stringName,</var></code> adalah <code><var>stringName</var>.length - 1</code>. Jika indeks yang anda berikan diluar <em>range </em>ini, maka JavaScript akan me-<em>return </em>string kosong.</p>
<p>Jika tidak ada indeks yang tertera pada <code>charAt()</code>, maka nilai defaultnya adalah <code>0</code>.</p>
<h2 id="Contoh">Contoh</h2>
<h3 id="Menampilkan_karakter_pada_lokasi_yang_berbeda_pada_sebuah_string">Menampilkan karakter pada lokasi yang berbeda pada sebuah string</h3>
<p>Contoh berikut menunjukan karakter di lokasi berbeda pada string "<code>Brave new world</code>":</p>
<pre class="brush: js notranslate">var anyString = 'Brave new world';
console.log("The character at index 0 is '" + anyString.charAt() + "'");
// Tidak ada angka yang tertera pada charAt(), maka digunakan nilai 0 sebagai <em>default</em>
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) + "'");
</pre>
<p>Baris kode di atas menghasilkan <em>output </em>sebagai berikut:</p>
<pre class="brush: js notranslate">The character at index 0 is 'B'
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 ''
</pre>
<h3 id="Mendapatkan_seluruh_karakter">Mendapatkan seluruh karakter</h3>
<p>Berikut ini adalah cara untuk memastikan bahwa melalui <em>loop </em>string selalu memberikan karakter utuh, meskipun string berisi karakter yang tidak ada dalam Bidang Multilingual Dasar.</p>
<pre class="brush: js notranslate">var str = 'A \uD87E\uDC04 Z'; // Kita bisa menggunakan karakter non-BMP
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 ''; // Posisi tidak ditemukan
}
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;
}
</pre>
<p>Pada <em>environment </em>ECMAScript 2016 yang memungkinkan assignment yang rusak, Berikut ini merupakan alternatif yang lebih ringkas dan fleksibel karena perulangan dilakukan penmabahan secara otomatis pada variabel <em>increment</em>/ penambahannya (jika karakter menjaminnya sebagai pasangan pengganti).</p>
<pre class="brush: js notranslate">let str = 'A\uD87E\uDC04Z' // Kita juga dapat menggunakan karakter non-BMP
for (let 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) {
let code = str.charCodeAt(i)
if (Number.isNaN(code)) {
return '' // Posisi tidak ditemukan
}
if (code < 0xD800 || code > 0xDFFF) {
return [str.charAt(i), i] // Karakter normal, 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'
}
let 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'
}
let 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]
}
</pre>
<h3 id="Memperbaiki_charAt_untuk_mendukung_karakter_non-Basic-Multilingual-Plane_BMP">Memperbaiki charAt() untuk mendukung karakter non-Basic-Multilingual-Plane (BMP)</h3>
<p>Pada contoh sebelumnya mungkin lebih berguna untuk program yang mendukung karakter non-BMP (karena tidak mengharuskan pemanggil untuk mengetahui dimana karakter non-BMP mungkin muncul), jika memang diinginkan, dalam memilih karakter dengan index, untuk memperlakukan pasangan pengganti dalam string sebagai karakter tunggal yang mereka wakili, dapat menggunakan yang berikut ini:</p>
<pre class="brush: js notranslate">function fixedCharAt(str, idx) {
let ret = ''
str += ''
let end = str.length
let surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g
while ((surrogatePairs.exec(str)) != null) {
let lastIdx = surrogatePairs.lastIndex
if (lastIdx - 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
}
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype.charat', 'String.prototype.charAt')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("javascript.builtins.String.charAt")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("String.prototype.indexOf()")}}</li>
<li>{{jsxref("String.prototype.lastIndexOf()")}}</li>
<li>{{jsxref("String.prototype.charCodeAt()")}}</li>
<li>{{jsxref("String.prototype.codePointAt()")}}</li>
<li>{{jsxref("String.prototype.split()")}}</li>
<li>{{jsxref("String.fromCodePoint()")}}</li>
<li><a href="https://mathiasbynens.be/notes/javascript-unicode">JavaScript has a Unicode problem – Mathias Bynens</a></li>
</ul>
|