diff options
Diffstat (limited to 'files/pl/web/javascript/referencje/obiekty/string/index.html')
-rw-r--r-- | files/pl/web/javascript/referencje/obiekty/string/index.html | 194 |
1 files changed, 0 insertions, 194 deletions
diff --git a/files/pl/web/javascript/referencje/obiekty/string/index.html b/files/pl/web/javascript/referencje/obiekty/string/index.html deleted file mode 100644 index ff27fbc81a..0000000000 --- a/files/pl/web/javascript/referencje/obiekty/string/index.html +++ /dev/null @@ -1,194 +0,0 @@ ---- -title: String -slug: Web/JavaScript/Referencje/Obiekty/String -tags: - - JavaScript - - String -translation_of: Web/JavaScript/Reference/Global_Objects/String ---- -<p>{{JSRef}}</p> - -<h2 id="Podsumowanie" name="Podsumowanie">Podsumowanie</h2> - -<p>Tworzy obiekt pozwalający działać na ciągach znaków.</p> - -<h2 id="Tworzony_przez" name="Tworzony_przez">Składnia</h2> - -<p>Literały znakowe są postaci:</p> - -<pre class="syntaxbox notranslate">'string text' -"string text" -"中文 español English हिन्दी العربية português বাংলা русский 日本語 ਪੰਜਾਬੀ 한국어 தமிழ்" -</pre> - -<p>Beside regular, printable characters, special characters can be encoded using escape notation:</p> - -<table class="standard-table"> - <thead> - <tr> - <th scope="col">Code</th> - <th scope="col">Output</th> - </tr> - </thead> - <tbody> - <tr> - <td><code>\0</code></td> - <td>the NUL character</td> - </tr> - <tr> - <td><code>\'</code></td> - <td>single quote</td> - </tr> - <tr> - <td><code>\"</code></td> - <td>double quote</td> - </tr> - <tr> - <td><code>\\</code></td> - <td>backslash</td> - </tr> - <tr> - <td><code>\n</code></td> - <td>new line</td> - </tr> - <tr> - <td><code>\r</code></td> - <td>carriage return</td> - </tr> - <tr> - <td><code>\v</code></td> - <td>vertical tab</td> - </tr> - <tr> - <td><code>\t</code></td> - <td>tab</td> - </tr> - <tr> - <td><code>\b</code></td> - <td>backspace</td> - </tr> - <tr> - <td><code>\f</code></td> - <td>form feed</td> - </tr> - <tr> - <td><code>\uXXXX</code></td> - <td>unicode codepoint</td> - </tr> - <tr> - <td><code>\xXX</code></td> - <td>the Latin-1 character</td> - </tr> - </tbody> -</table> - -<p>Or, using the <code>String</code> global object directly:</p> - -<pre class="syntaxbox notranslate">String(thing) -new String(thing) -</pre> - -<h3 id="Parametry" name="Parametry">Parametry</h3> - -<dl> - <dt><code>thing</code></dt> - <dd>Dowolny łańcuch znaków.</dd> -</dl> - -<h2 id="Opis" name="Opis">Opis</h2> - -<p>trings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their {{jsxref("String.length", "length")}}, to build and concatenate them using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/String_Operators">+ and += string operators</a>, checking for the existence or location of substrings with the {{jsxref("String.prototype.indexOf()", "indexOf()")}} method, or extracting substrings with the {{jsxref("String.prototype.substring()", "substring()")}} method.</p> - -<h3 id="Character_access" name="Character_access">Character access</h3> - -<p>There are two ways to access an individual character in a string. The first is the {{jsxref("String.prototype.charAt()", "charAt()")}} method:</p> - -<pre class="brush: js notranslate">return 'cat'.charAt(1); // returns "a"</pre> - -<div class="line-number" style="top: 0px;"></div> - -<p>The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:</p> - -<pre class="brush: js notranslate">return 'cat'[1]; // returns "a"</pre> - -<div class="line-number" style="top: 0px;"></div> - -<p>For character access using bracket notation, attempting to delete or assign a value to these properties will not succeed. The properties involved are neither writable nor configurable. (See {{jsxref("Object.defineProperty()")}} for more information.)</p> - -<h3 id="Comparing_strings" name="Comparing_strings">Comparing strings</h3> - -<p>C developers have the <code>strcmp()</code> function for comparing strings. In JavaScript, you just use the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">less-than and greater-than operators</a>:</p> - -<pre class="brush: js notranslate">var a = 'a'; -var b = 'b'; -if (a < b) { // true - print(a + ' is less than ' + b); -} else if (a > b) { - print(a + ' is greater than ' + b); -} else { - print(a + ' and ' + b + ' are equal.'); -}</pre> - -<p>A similar result can be achieved using the {{jsxref("String.prototype.localeCompare()", "localeCompare()")}} method inherited by <code>String</code> instances.</p> - -<h3 id="Distinction_between_string_primitives_and_String_objects">Distinction between string primitives and <code>String</code> objects</h3> - -<p>Note that JavaScript distinguishes between <code>String</code> objects and primitive string values. (The same is true of {{jsxref("Global_Objects/Boolean", "Boolean")}} and {{jsxref("Global_Objects/Number", "Numbers")}}.)</p> - -<p>String literals (denoted by double or single quotes) and strings returned from <code>String</code> calls in a non-constructor context (i.e., without using the {{jsxref("Operators/new", "new")}} keyword) are primitive strings. JavaScript automatically converts primitives to <code>String</code> objects, so that it's possible to use <code>String</code> object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.</p> - -<pre class="brush: js notranslate">var s_prim = 'foo'; -var s_obj = new String(s_prim); - -console.log(typeof s_prim); // Logs "string" -console.log(typeof s_obj); // Logs "object"</pre> - -<div class="line-number" style="top: 76px;"></div> - -<p>String primitives and <code>String</code> objects also give different results when using {{jsxref("Global_Objects/eval", "eval()")}}. Primitives passed to <code>eval</code> are treated as source code; <code>String</code> objects are treated as all other objects are, by returning the object. For example:</p> - -<pre class="brush: js notranslate">var s1 = '2 + 2'; // creates a string primitive -var s2 = new String('2 + 2'); // creates a String object -console.log(eval(s1)); // returns the number 4 -console.log(eval(s2)); // returns the string "2 + 2"</pre> - -<p>For these reasons, code may break when it encounters <code>String</code> objects when it expects a primitive string instead, although generally authors need not worry about the distinction.</p> - -<p>A <code>String</code> object can always be converted to its primitive counterpart with the {{jsxref("String.prototype.valueOf()", "valueOf()")}} method.</p> - -<pre class="brush: js notranslate">console.log(eval(s2.valueOf())); // returns the number 4</pre> - -<div class="note"><strong>Note:</strong> For another possible approach to strings in JavaScript, please read the article about <a href="https://developer.mozilla.org/en-US/Add-ons/Code_snippets/StringView"><code>StringView</code> — a C-like representation of strings based on typed arrays</a>.</div> - -<h2 id="W.C5.82asno.C5.9Bci" name="W.C5.82asno.C5.9Bci">Własności</h2> - -<dl> - <dt>{{jsxref("String.prototype")}}</dt> - <dd>Pozwala na dodawanie własności do obiektu <code>String</code>.</dd> -</dl> - -<div>{{jsOverrides("Function", "Properties", "prototype")}}</div> - -<h2 id="Metody">Metody</h2> - -<dl> - <dt>{{jsxref("String.fromCharCode()")}}</dt> - <dd>Zwraca łańcuch znaków stworzony przez podaną sekwencję kodów Unicode.</dd> - <dt>{{jsxref("String.fromCodePoint()")}} {{experimental_inline}}</dt> - <dd>Returns a string created by using the specified sequence of code points.</dd> - <dt>{{jsxref("String.raw()")}} {{experimental_inline}}</dt> - <dd>Returns a string created from a raw template string.</dd> -</dl> - -<div>{{jsOverrides("Function", "Methods", "fromCharCode", "fromCodePoint", "raw")}}</div> - -<h2 id="Przyk.C5.82ady" name="Przyk.C5.82ady">Przykłady</h2> - -<h3 id="Example_String_conversion">Example: String conversion</h3> - -<p>It's possible to use <code>String</code> as a "safer" {{jsxref("String.prototype.toString()", "toString()")}} alternative, as although it still normally calls the underlying <code>toString()</code>, it also works for {{jsxref("null")}} and {{jsxref("undefined")}}. For example:</p> - -<pre class="brush: js notranslate">var outputStrings = []; -for (var i = 0, n = inputValues.length; i < n; ++i) { - outputStrings.push(String(inputValues[i])); -}</pre> |