1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
---
title: String.prototype.substring()
slug: Web/JavaScript/Reference/Global_Objects/String/substring
tags:
- Funktion
- JavaScript
- Method
- Methode(2)
- Méthode
- Prototype
- Reference
- Referenz
- Référence(2)
- String
- Zeichenkette
translation_of: Web/JavaScript/Reference/Global_Objects/String/substring
---
<div>{{JSRef("Global_Objects", "String")}}</div>
<h2 id="Summary" name="Summary">Definition</h2>
<p>Die <strong><code>substring()</code></strong> Methode gibt einen Teilstring eines <code>Strings</code> zwischen einem Index und einem anderen, oder dem Ende der Zeichenkette zurück.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox"><code><var>str</var>.substring(<var>indexA</var>[, <var>indexB</var>])</code></pre>
<h3 id="Parameters" name="Parameters">Parameter</h3>
<dl>
<dt><code>indexA</code></dt>
<dd>Ein Integer zwischen <code>0</code> und der Länge der Zeichenkette, welcher das erste Zeichen des Teilstrings repräsentiert.</dd>
<dt><code>indexB</code></dt>
<dd>Optional. Ein Integer zwischen <code>0</code> und der Länge der Zeichenkette, welcher das erste nicht im Teilstring enthaltene Zeichen darstellt.</dd>
</dl>
<h2 id="Description" name="Description">Beschreibung</h2>
<p><code>substring()</code> extrahiert Zeichen von <code>indexA</code> bis nicht einschließlich <code>indexB</code>. Im Detail:</p>
<ul>
<li>Falls <code>indexA</code> gleich <code>indexB ist</code>, gibt <code>substring()</code> einen leeren String zurück.</li>
<li>Falls <code>indexB</code> nicht angegeben ist, extrahiert <code>substring()</code> alle Zeichen bis zum Ende der Zeichenkette.</li>
<li>Falls eines der Argumente kleiner als 0 oder {{jsxref("Global_Objects/NaN", "NaN")}} ist, wird dieser als 0 interpretiert.</li>
<li>Falls eines der Argumente größer als die Länge der Zeichenkette ist, wird dieses als <code>stringName.length behandelt</code>.</li>
</ul>
<p>Falls <code>indexA</code> größer als <code>indexB ist</code>, werden die Argumente vertauscht <code>substring()</code>; zum Beispiel, <code><em>str</em>.substring(1, 0) == <em>str</em>.substring(0, 1)</code>.</p>
<h2 id="Examples" name="Examples">Beispiele</h2>
<h3 id="Example:_Using_substring" name="Example:_Using_substring">Beispiel: Benutzung von <code>substring()</code></h3>
<p>Das folgende Beispiel benutzt <code>substring()</code>, um Zeichen des Strings <code>'Mozilla' anzuzeigen</code>:</p>
<pre class="brush: js">var anyString = 'Mozilla';
// Zeigt 'Moz'
console.log(anyString.substring(0, 3));
console.log(anyString.substring(3, 0));
// Zeigt 'lla'
console.log(anyString.substring(4, 7));
console.log(anyString.substring(7, 4));
// Zeigt 'Mozill'
console.log(anyString.substring(0, 6));
// Zeigt 'Mozilla'
console.log(anyString.substring(0, 7));
console.log(anyString.substring(0, 10));
</pre>
<h3 id="Example:_Using_substring_with_length_property" name="Example:_Using_substring_with_length_property">Beispiel: Benutzung von <code>substring()</code> mit <code>length</code> Eigenschaft</h3>
<p>Das folgende Beispiel benutzt die <code>substring()</code> Methode und die {{jsxref("String.length", "length")}} Eigenschaft, um die letzten Zeichen des Strings zu extrahieren. Diese Methode ist leichter zu merken, da die genauen Start- und Endparameter nicht bekannt sein müssen.</p>
<pre class="brush: js">// Zeigt 'illa' - die letzten 4 Zeichen
var anyString = 'Mozilla';
var anyString4 = anyString.substring(anyString.length - 4);
console.log(anyString4);
// Zeigt 'zilla' - die letzten 5 Zeichen
var anyString = 'Mozilla';
var anyString5 = anyString.substring(anyString.length - 5);
console.log(anyString5);
</pre>
<h3 id="Example:_Replacing_a_substring_within_a_string" name="Example:_Replacing_a_substring_within_a_string">Beispiel: Ersetze ein Teilstring mit einem String</h3>
<p>Das folgende Beispiel ersetzt ein Teilstring mit einem String. Es werden sowohhl einzelne Zeichen als auch Teilstrings ersetzt. Der Funktionsaufruf am Ende des Beispiel ändert die Zeichenkette <code>'Brave New World'</code> in <code>'Brave New Web'</code>.</p>
<pre class="brush: js">// Ersetzt oldS mit newS in der Zeichenkette fullS
function replaceString(oldS, newS, fullS) {
for (var i = 0; i < 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>Beachte, dass dieses Beispiel auch in einer Endlosschleife resultieren kann, wenn <code>oldS</code> ein Teilstring von <code>newS</code> ist — zum Beispiel, falls 'World' mit 'OtherWorld' ersetzt wird. Der folgende Code zeigt ein bessere Methode Strings zu ersetzen:</p>
<pre class="brush: js">function replaceString(oldS, newS, fullS) {
return fullS.split(oldS).join(newS);
}
</pre>
<p>Der oben stehende Code zeigt ein Beispiel für die Verwendung der substring Operation. Falls Teilstrings ersetzt werden, kann in den meisten Fällen {{jsxref("String.prototype.replace()")}} verwendet werden.</p>
<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>ECMAScript 1. Edition.</td>
<td>Standard</td>
<td>In JavaScript 1.0 implementiert.</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>
</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>Standard 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>Standard 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" name="See_also">Siehe auch</h2>
<ul>
<li>{{jsxref("String.prototype.substr()")}}</li>
<li>{{jsxref("String.prototype.slice()")}}</li>
</ul>
|