aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/string/charat
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:41:15 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:41:15 -0500
commit4b1a9203c547c019fc5398082ae19a3f3d4c3efe (patch)
treed4a40e13ceeb9f85479605110a76e7a4d5f3b56b /files/de/web/javascript/reference/global_objects/string/charat
parent33058f2b292b3a581333bdfb21b8f671898c5060 (diff)
downloadtranslated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.gz
translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.bz2
translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.zip
initial commit
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/string/charat')
-rw-r--r--files/de/web/javascript/reference/global_objects/string/charat/index.html305
1 files changed, 305 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/string/charat/index.html b/files/de/web/javascript/reference/global_objects/string/charat/index.html
new file mode 100644
index 0000000000..815eb59bba
--- /dev/null
+++ b/files/de/web/javascript/reference/global_objects/string/charat/index.html
@@ -0,0 +1,305 @@
+---
+title: String.prototype.charAt()
+slug: Web/JavaScript/Reference/Global_Objects/String/charAt
+tags:
+ - JavaScript
+ - Method
+ - Prototype
+ - Reference
+ - String
+translation_of: Web/JavaScript/Reference/Global_Objects/String/charAt
+---
+<div>{{JSRef}}</div>
+
+<p>Die <strong><code>charAt()</code></strong>-Methode gibt das Zeichen an einer bestimmten Stelle eines Strings wieder.</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox"><var>str</var>.charAt(<var>index</var>)</pre>
+
+<h3 id="Parameter">Parameter</h3>
+
+<dl>
+ <dt><code>index</code></dt>
+ <dd>Eine Zahl zwischen 0 und <code>str.length-1</code>. Ist kein <code>index</code> gegeben, ist dieser automatisch 0.</dd>
+</dl>
+
+<h3 id="Wiedergegebener_Wert">Wiedergegebener Wert</h3>
+
+<p>Ein String, der das Zeichen an der angegebenen <code>index</code>-Stelle wiedergibt. Wenn <code>index</code> außerhalb der Reichweite liegt, wird ein leerer String zurückgegeben.<br>
+  </p>
+
+<h2 id="Beschreibung">Beschreibung</h2>
+
+<p>Zeichen in einem String werden von links nach rechts indexiert. Der Index des ersten Zeichens ist 0, der Index des letzten Zeichens eines Strings namens <code>stringName</code> ist<code> stringName.length - 1</code>.</p>
+
+<p>Wenn kein Index bereitgestellt wird, ist dieser standardmäßig 0.</p>
+
+<h2 id="Beispiele">Beispiele</h2>
+
+<h3 id="Wiedergabe_von_Zeichen_an_verschiedenen_Stellen_eines_Strings">Wiedergabe von Zeichen an verschiedenen Stellen eines Strings</h3>
+
+<p>Das folgende Beispiel gibt Zeichen an verschiedenen Orten im String "<code>Brave new world</code>" wieder:</p>
+
+<pre class="brush: js">var anyString = 'Brave new world';
+console.log("Das Zeichen bei Index 0 ist '" + anyString.charAt() + "'");
+// kein Index definiert, 0 als Standard genutzt
+
+console.log("Das Zeichen bei Index 0 ist '" + anyString.charAt(0) + "'");
+console.log("Das Zeichen bei Index 1 ist '" + anyString.charAt(1) + "'");
+console.log("Das Zeichen bei Index 2 ist '" + anyString.charAt(2) + "'");
+console.log("Das Zeichen bei Index 3 ist '" + anyString.charAt(3) + "'");
+console.log("Das Zeichen bei Index 4 ist '" + anyString.charAt(4) + "'");
+console.log("Das Zeichen bei Index 999 ist '" + anyString.charAt(999) + "'");
+</pre>
+
+<p>Diese Zeilen geben folgendes wieder:</p>
+
+<pre class="brush: js">Das Zeichen bei Index 0 ist 'B'
+Das Zeichen bei Index 0 ist 'B'
+Das Zeichen bei Index 1 ist 'r'
+Das Zeichen bei Index 2 ist 'a'
+Das Zeichen bei Index 3 ist 'v'
+Das Zeichen bei Index 4 ist 'e'
+Das Zeichen bei Index 999 ist ''
+</pre>
+
+<h3 id="Erhalten_von_vollständigen_Zeichen">Erhalten von vollständigen Zeichen</h3>
+
+<p>Der folgende Code versichert, dass der Gebrauch eines String-Loops immer das vollständige Zeichen wiedergibt - auch wenn der String Zeichen enthält, die nicht auf der mehrsprachigen Basisebene (MBE) vorhanden sind.</p>
+
+<pre class="brush: js">var str = 'A \uD87E\uDC04 Z'; // direkter Einsatz von nicht MBE-Zeichen auch möglich
+for (var i = 0, chr; i &lt; str.length; i++) {
+ if ((chr = getWholeChar(str, i)) === false) {
+ continue;
+ }
+ // Übernehme diese Zeile zu Beginn jedes Loops,
+ // gib den kompletten String und Iterator an
+ // und gebe eine Variable wieder, die das individuelle Zeichen wiederspiegelt.
+
+ console.log(chr);
+}
+
+function getWholeChar(str, i) {
+ var code = str.charCodeAt(i);
+
+ if (Number.isNaN(code)) {
+ return ''; // Position not found
+ }
+ if (code &lt; 0xD800 || code &gt; 0xDFFF) {
+ return str.charAt(i);
+ }
+
+ // High surrogate (could change last hex to 0xDB7F to treat high private
+ // surrogates as single characters)
+ if (0xD800 &lt;= code &amp;&amp; code &lt;= 0xDBFF) {
+ if (str.length &lt;= (i + 1)) {
+ throw 'High surrogate without following low surrogate';
+ }
+ var next = str.charCodeAt(i + 1);
+ if (0xDC00 &gt; next || next &gt; 0xDFFF) {
+ throw 'High surrogate without following low surrogate';
+ }
+ return str.charAt(i) + str.charAt(i + 1);
+ }
+ // Low surrogate (0xDC00 &lt;= code &amp;&amp; code &lt;= 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 &gt; prev || prev &gt; 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>In einer ECMAScript 2016-Umgebung, welche destruktive Benennung erlaubt, ist eine bündigere und  zu gewissem Grad flexiblere Alternative möglich, da es automatisch eine Erhöhung der entsprechenden Variable vornimmt (wenn das Zeichen<br>
+ ein Ersatzpaar ist).</p>
+
+<pre class="brush: js">var str = 'A\uD87E\uDC04Z'; // direkter Einsatz von nicht MBE-Zeichen auch möglich
+for (var i = 0, chr; i &lt; 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)
+ // Übernehme diese Zeile zu Beginn jedes Loops,
+ // gib den kompletten String und Iterator an
+ // und gebe einen Array wieder, der die individuellen Zeichen und 'i'-Wert wiederspiegelt
+ // (nur geändert, wenn Ersatzpaar).
+
+ console.log(chr);
+}
+
+function getWholeCharAndI(str, i) {
+ var code = str.charCodeAt(i);
+
+ if (Number.isNaN(code)) {
+ return ''; // Position nicht gefunden
+ }
+ if (code &lt; 0xD800 || code &gt; 0xDFFF) {
+ return [str.charAt(i), i]; // Normales Zeichen, 'i' bleibt gleich
+ }
+
+ // High surrogate (could change last hex to 0xDB7F to treat high private
+ // surrogates as single characters)
+ if (0xD800 &lt;= code &amp;&amp; code &lt;= 0xDBFF) {
+ if (str.length &lt;= (i + 1)) {
+ throw 'High surrogate without following low surrogate';
+ }
+ var next = str.charCodeAt(i + 1);
+ if (0xDC00 &gt; next || next &gt; 0xDFFF) {
+ throw 'High surrogate without following low surrogate';
+ }
+ return [str.charAt(i) + str.charAt(i + 1), i + 1];
+ }
+ // Low surrogate (0xDC00 &lt;= code &amp;&amp; code &lt;= 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 &gt; prev || prev &gt; 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="Korrigiere_charAt()_um_Zeichen_der_nicht_mehrsprachigen_Basisebene_(MBE)_zu_unterstützen">Korrigiere <code>charAt()</code>, um Zeichen der nicht mehrsprachigen Basisebene (MBE) zu unterstützen</h3>
+
+<p><br>
+  Das obige Beispiel dürfte häufiger für diejenigen hilfreich sein, die nicht MBE-Zeichen unterstützen möchten (da man nicht wissen muss, wo nicht MBE-Zeichen vorkommen).  Falls man wünscht (durch Angabe eines Indexwerts) die Ersatzpaare innerhalb eines Strings als Einzelcharakter zu behandeln, kann man folgendes nutzen:</p>
+
+<pre class="brush: js">function fixedCharAt(str, idx) {
+ var ret = '';
+ str += '';
+ var end = str.length;
+
+ var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
+ while ((surrogatePairs.exec(str)) != null) {
+ var li = surrogatePairs.lastIndex;
+ if (li - 2 &lt; idx) {
+ idx++;
+ } else {
+ break;
+ }
+ }
+
+ if (idx &gt;= end || idx &lt; 0) {
+ return '';
+ }
+
+ ret += str.charAt(idx);
+
+ if (/[\uD800-\uDBFF]/.test(ret) &amp;&amp; /[\uDC00-\uDFFF]/.test(str.charAt(idx + 1))) {
+ // Gehe eins weiter, da eins der Zeichen Teil eines Ersatzpaares ist
+ ret += str.charAt(idx + 1);
+ }
+ return ret;
+}
+</pre>
+
+<h2 id="Spezifikationen">Spezifikationen</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Spezifikation</th>
+ <th scope="col">Status</th>
+ <th scope="col">Kommentar</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES1')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>erste Definition</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-15.5.4.4', 'String.prototype.charAt')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-string.prototype.charat', 'String.prototype.charAt')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-string.prototype.charat', 'String.prototype.charAt')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser-Kompatibilität">Browser-Kompatibilität</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>Standardunterstützung</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 für Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td> Standardunterstützung</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="Siehe_auch">Siehe auch</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>