diff options
| author | Ryan Johnson <rjohnson@mozilla.com> | 2021-04-29 16:16:42 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-29 16:16:42 -0700 |
| commit | 95aca4b4d8fa62815d4bd412fff1a364f842814a (patch) | |
| tree | 5e57661720fe9058d5c7db637e764800b50f9060 /files/id/web/javascript/reference/global_objects/string/charat | |
| parent | ee3b1c87e3c8e72ca130943eed260ad642246581 (diff) | |
| download | translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.tar.gz translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.tar.bz2 translated-content-95aca4b4d8fa62815d4bd412fff1a364f842814a.zip | |
remove retired locales (#699)
Diffstat (limited to 'files/id/web/javascript/reference/global_objects/string/charat')
| -rw-r--r-- | files/id/web/javascript/reference/global_objects/string/charat/index.html | 238 |
1 files changed, 0 insertions, 238 deletions
diff --git a/files/id/web/javascript/reference/global_objects/string/charat/index.html b/files/id/web/javascript/reference/global_objects/string/charat/index.html deleted file mode 100644 index e464f2bd4c..0000000000 --- a/files/id/web/javascript/reference/global_objects/string/charat/index.html +++ /dev/null @@ -1,238 +0,0 @@ ---- -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> |
