aboutsummaryrefslogtreecommitdiff
path: root/files/id/web/javascript/reference/global_objects/string/split/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/id/web/javascript/reference/global_objects/string/split/index.html')
-rw-r--r--files/id/web/javascript/reference/global_objects/string/split/index.html241
1 files changed, 241 insertions, 0 deletions
diff --git a/files/id/web/javascript/reference/global_objects/string/split/index.html b/files/id/web/javascript/reference/global_objects/string/split/index.html
new file mode 100644
index 0000000000..21f19d353f
--- /dev/null
+++ b/files/id/web/javascript/reference/global_objects/string/split/index.html
@@ -0,0 +1,241 @@
+---
+title: String.prototype.split()
+slug: Web/JavaScript/Reference/Global_Objects/String/split
+tags:
+ - JavaScript
+ - Method
+ - Prototype
+ - Reference
+ - Regular Expressions
+ - String
+translation_of: Web/JavaScript/Reference/Global_Objects/String/split
+---
+<div>{{JSRef}}</div>
+
+<p>Method <strong><code>split()</code></strong> membagi sebuah objek {{jsxref("String")}} ke sebuah array string dengan memisahkannya menjadi substring.</p>
+
+<h2 id="Sintaks">Sintaks</h2>
+
+<pre class="syntaxbox"><code><var>str</var>.split([<var>separator</var>[, <var>limit</var>]])</code></pre>
+
+<h3 id="Parameter">Parameter</h3>
+
+<dl>
+ <dt><code>separator</code></dt>
+ <dd>Opsional. Menentukan karakter yang digunakan untuk memisahkan string. <code>separator</code> dapat berupa string atau sebuah {{jsxref("Global_Objects/RegExp", "regular expression", "", 1)}}. Jika <code>separator</code> dihilangkan / tidak digunakan, array yang dikembalikan akan terdiri dari satu elemen yang berisi seluruh string. Jika <code>separator</code> dikosongkan atau empty string, <code>str</code> akan dikonversikan sebagai sebuah array dari karakter.</dd>
+ <dt><code>limit</code></dt>
+ <dd>
+ <p>Opsional. Nilai integer yang digunakan untuk menentukan jumlah batas yang dapat ditemukan. Method <code>split()</code> tetap membagi pada setiap kecocokan pada <code>separator</code>, sampai jumlah pembagi item sama dengan <code>limit</code> atau string jatuh lebih pendek dari <code>separator</code>.</p>
+ </dd>
+</dl>
+
+<h2 id="Deskripsi">Deskripsi</h2>
+
+<p>Method <code>split()</code> mengembalikan array baru.</p>
+
+<p>Saat ditemukan, <code>separator</code> akan dihapus dari string dan substrings akan di kembalikan ke dalam array. Jika <code>separator</code> tidak ditemukan atau di hilangkan, array terdiri satu elemen array yang terdiri dari keseluruhan string. Jika <code>separator</code> merupakan empty string, <code>str</code> dikonversi menjadi sebuah array karakter.</p>
+
+<p>Jika <code>separator</code> adalah ekspesi reguler yang terdapat tanda kurung , maka setiap kali <code>separator</code> cocok, hasilnya (termasuk hasil yang tidak didefinisikan) dari penangkap tanda kurung akan di sambungkan ke dalam output array. Namun, tidak semua browser mendukung kemampuan ini.</p>
+
+<p>{{Note("Ketika string kosong, method <code>split()</code> mengembalikan array berisi satu string kosong, dari pada array kosong. Jika string dan separator keduanya string kosong, array kosong akan dikembalikan.")}}</p>
+
+<h2 id="Contoh">Contoh</h2>
+
+<h3 id="Penggunaan_split()">Penggunaan <code>split()</code></h3>
+
+<p>Contoh berikut menjelaskan fungsi yang membagi string ke dalam sebuah array string menggunakan separator tertentu. Setelah memisahkan string , fungsi menampilkan pesan yang menunjukan string asli (sebelum dibagi), separator yang digunakan, jumlah elemen pada array, dan elemen array secara individual.</p>
+
+<pre class="brush: js">function splitString(stringToSplit, separator) {
+ var arrayOfStrings = stringToSplit.split(separator);
+
+ console.log('The original string is: "' + stringToSplit + '"');
+ console.log('The separator is: "' + separator + '"');
+ console.log('The array has ' + arrayOfStrings.length + ' elements: ' + arrayOfStrings.join(' / '));
+}
+
+var tempestString = 'Oh brave new world that has such people in it.';
+var monthString = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';
+
+var space = ' ';
+var comma = ',';
+
+splitString(tempestString, space);
+splitString(tempestString);
+splitString(monthString, comma);
+</pre>
+
+<p>Contoh ini menghasilkan output berikut:</p>
+
+<pre>The original string is: "Oh brave new world that has such people in it."
+The separator is: " "
+The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it.
+
+The original string is: "Oh brave new world that has such people in it."
+The separator is: "undefined"
+The array has 1 elements: Oh brave new world that has such people in it.
+
+The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
+The separator is: ","
+The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec
+</pre>
+
+<h3 id="Menghapus_spasi_dari_string">Menghapus spasi dari string</h3>
+
+<p>Pada contoh berikut, <code>split()</code> mencari 0 atau lebih spasi diikuti semikolon, dan di ikuti 0 atau lebih spasi dan, saat ditemukan, menghapus spasi dari string. <code>nameList</code> merupakan array yang dikembalikan dari hasil <code>split()</code>.</p>
+
+<pre class="brush: js">var names = 'Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ';
+
+console.log(names);
+
+var re = /\s*;\s*/;
+var nameList = names.split(re);
+
+console.log(nameList);
+</pre>
+
+<p>Dua baris log ini; log baris pertama string asli, dan log baris kedua array yang dihasilkan.</p>
+
+<pre>Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
+[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand " ]
+</pre>
+
+<h3 id="Mengembalikan_batas_jumlah_pembagi">Mengembalikan batas jumlah pembagi</h3>
+
+<p>Pada contoh berikut, <code>split()</code> mencari 0 atau lebih spasi didalam string dan mengembalikan 3 pembagian pertama yang ditemukan.</p>
+
+<pre class="brush: js">var myString = 'Hello World. How are you doing?';
+var splits = myString.split(' ', 3);
+
+console.log(splits);
+</pre>
+
+<p>Script ini akan menampilkan seperti berikut:</p>
+
+<pre>Hello,World.,How
+</pre>
+
+<h3 id="Tanda_kurung">Tanda kurung</h3>
+
+<p>Jika <code>separator</code> terdapat tanda kurung, hasil yang cocok akan dikembalikan ke dalam array.</p>
+
+<pre class="brush: js">var myString = 'Hello 1 word. Sentence number 2.';
+var splits = myString.split(/(\d)/);
+
+console.log(splits);
+</pre>
+
+<p>Script tersebut menampilkan seperti berikut:</p>
+
+<pre>[ 'Hello ', '1', ' word. Sentence number ', '2', '.' ]
+</pre>
+
+<h3 id="Membalikkan_String_menggunakan_split()">Membalikkan String menggunakan <code>split()</code></h3>
+
+<pre class="brush: js">var str = 'asdfghjkl';
+var strReverse = str.split('').reverse().join(''); // 'lkjhgfdsa'
+// split() returns an array on which reverse() and join() can be applied
+</pre>
+
+<p><strong>Bonus:</strong> Gunakan operator {{jsxref("Operators/Comparison_Operators", "===", "#Identity_strict_equality_(===)")}} untuk mengetahui apakah string asli adalah palindrome.</p>
+
+<h2 id="Spesifikasi">Spesifikasi</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Spesifikasi</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES3')}}</td>
+ <td>{{Spec2('ES3')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.1.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.5.4.14', 'String.prototype.split')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-string.prototype.split', 'String.prototype.split')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-string.prototype.split', 'String.prototype.split')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Kompabilitas_Browser">Kompabilitas Browser</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Fitur</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Dukungan dasar</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Fitur</th>
+ <th>Android</th>
+ <th>Chrome for Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>
+ <table class="compat-table">
+ <tbody>
+ <tr>
+ <td>Dukungan dasar</td>
+ <td> </td>
+ </tr>
+ </tbody>
+ </table>
+ </td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<h2 id="Lihat_Juga">Lihat Juga</h2>
+
+<ul>
+ <li>{{jsxref("String.prototype.charAt()")}}</li>
+ <li>{{jsxref("String.prototype.indexOf()")}}</li>
+ <li>{{jsxref("String.prototype.lastIndexOf()")}}</li>
+ <li>{{jsxref("Array.prototype.join()")}}</li>
+</ul>