aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/global_objects/string/substring/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/vi/web/javascript/reference/global_objects/string/substring/index.html
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/vi/web/javascript/reference/global_objects/string/substring/index.html')
-rw-r--r--files/vi/web/javascript/reference/global_objects/string/substring/index.html195
1 files changed, 195 insertions, 0 deletions
diff --git a/files/vi/web/javascript/reference/global_objects/string/substring/index.html b/files/vi/web/javascript/reference/global_objects/string/substring/index.html
new file mode 100644
index 0000000000..e53b920581
--- /dev/null
+++ b/files/vi/web/javascript/reference/global_objects/string/substring/index.html
@@ -0,0 +1,195 @@
+---
+title: String.prototype.substring()
+slug: Web/JavaScript/Reference/Global_Objects/String/substring
+translation_of: Web/JavaScript/Reference/Global_Objects/String/substring
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>substring()</code></strong> phương thức trả về chuỗi con của 1 chuỗi bắt đầu từ vị trí bắt đầu đến vị trí kết thúc, hoặc đến cuối chuỗi nếu không có vị trí kết thúc</p>
+
+<h2 id="Cú_pháp">Cú pháp</h2>
+
+<pre class="syntaxbox notranslate"><code><var>str</var>.substring(<var>indexStart</var>[, <var>indexEnd</var>])</code></pre>
+
+<h3 id="Parameters">Parameters</h3>
+
+<dl>
+ <dt><code><var>indexStart</var></code></dt>
+ <dd>Một số integer giữa 0 và một số nhỏ hơn độ dài chuỗi, xác định vị trí kí tự đầu tiên trong chuỗi gốc để đưa vào chuỗi con.</dd>
+ <dt><code>indexEnd</code></dt>
+ <dd>Không bắt buộc. Một số integer giữa 0 và độ dài chuỗi. Chuỗi con không bao gồm ký tự ở vị trí indexEnd.</dd>
+</dl>
+
+<h3 id="Return_value">Return value</h3>
+
+<p>Chuỗi con trả về là chuỗi nằm ở vị trí từ indexStart đến vị trí ( indexEnd - 1 )</p>
+
+<h2 id="Description">Description</h2>
+
+<p><code>substring()</code> lấy ký tự từ vị trí <code>indexStart</code> tới vị trí (nhưng không bao gồm) <code>indexEnd</code>. Đặc biệt:</p>
+
+<ul>
+ <li>Nếu <code><var>indexStart bằng</var></code> <code><var>indexEnd</var></code>, <code>substring()</code> trả về 1 chuỗi rỗng.</li>
+ <li>Nếu không có <code>indexEnd</code>, <code>substring()</code> sẽ lấy từ vị trí bắt đầu đến cuối chuỗi. <em>(điều này giống với hàm substr()).</em></li>
+ <li>Nếu 1 trong 2 giá trị nhỏ hơn 0 hoặc là {{jsxref("NaN")}}, nó sẽ được xử lý như là 0.</li>
+ <li>Nếu 1 trong 2 giá trị lớn hơn  <code>stringName.length</code>, nó sẽ được xử lý như là <code>stringName.length</code>.</li>
+</ul>
+
+<p>Nếu <code>indexStart</code> lớn hơn <code>indexEnd</code>, chúng se được đổi chỗ; ví dụ, <code><em>str</em>.substring(1, 0) == <em>str</em>.substring(0, 1)</code>.</p>
+
+<h2 id="Examples">Examples</h2>
+
+<h3 id="Using_substring">Using <code>substring()</code></h3>
+
+<p>The following example uses <code>substring()</code> to display characters from the string <code>'Mozilla'</code>:</p>
+
+<pre class="brush: js notranslate">var anyString = 'Mozilla';
+
+// Displays 'Moz'
+console.log(anyString.substring(0, 3));
+console.log(anyString.substring(3, 0));
+
+// Displays 'lla'
+console.log(anyString.substring(4, 7));
+console.log(anyString.substring(4));
+console.log(anyString.substring(7, 4));
+
+// Displays 'Mozill'
+console.log(anyString.substring(0, 6));
+
+// Displays 'Mozilla'
+console.log(anyString.substring(0, 7));
+console.log(anyString.substring(0, 10));
+</pre>
+
+<h3 id="Using_substring_with_length_property">Using <code>substring()</code> with <code>length</code> property</h3>
+
+<p>The following example uses the <code>substring()</code> method and {{jsxref("String.length", "length")}} property to extract the last characters of a particular string. This method may be easier to remember, given that you don't need to know the starting and ending indices as you would in the above examples.</p>
+
+<pre class="brush: js notranslate">// Displays 'illa' the last 4 characters
+var anyString = 'Mozilla';
+var anyString4 = anyString.substring(anyString.length - 4);
+console.log(anyString4);
+
+// Displays 'zilla' the last 5 characters
+var anyString = 'Mozilla';
+var anyString5 = anyString.substring(anyString.length - 5);
+console.log(anyString5);
+</pre>
+
+<h3 id="Replacing_a_substring_within_a_string">Replacing a substring within a string</h3>
+
+<p>The following example replaces a substring within a string. It will replace both individual characters and substrings. The function call at the end of the example changes the string <code>'Brave New World'</code> into <code>'Brave New Web'</code>.</p>
+
+<pre class="brush: js notranslate">// Replaces oldS with newS in the string fullS
+function replaceString(oldS, newS, fullS) {
+ for (var i = 0; i &lt; fullS.length; ++i) {
+ if (fullS.substring(i, i + oldS.length) == oldS) {
+ fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
+ }
+ }
+ return fullS;
+}
+
+replaceString('World', 'Web', 'Brave New World');
+</pre>
+
+<p>Note that this can result in an infinite loop if <code>oldS</code> is itself a substring of <code>newS</code> — for example, if you attempted to replace 'World' with 'OtherWorld' here. A better method for replacing strings is as follows:</p>
+
+<pre class="brush: js notranslate">function replaceString(oldS, newS, fullS) {
+ return fullS.split(oldS).join(newS);
+}
+</pre>
+
+<p>The code above serves as an example for substring operations. If you need to replace substrings, most of the time you will want to use {{jsxref("String.prototype.replace()")}}.</p>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES1')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>Implemented in JavaScript 1.0.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.5.4.15', 'String.prototype.substring')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-string.prototype.substring', 'String.prototype.substring')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-string.prototype.substring', 'String.prototype.substring')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Basic support</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>Feature</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>Basic support</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="See_also">See also</h2>
+
+<ul>
+ <li>{{jsxref("String.prototype.substr()")}}</li>
+ <li>{{jsxref("String.prototype.slice()")}}</li>
+</ul>