blob: df264d00789038e4b0848594644b02d74b706117 (
plain)
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
|
---
title: String.prototype.trimStart()
slug: Web/JavaScript/Reference/Global_Objects/String/trimStart
tags:
- JavaScript
- Method
- Prototype
- Reference
- String
- Polyfill
browser-compat: javascript.builtins.String.trimStart
translation_of: Web/JavaScript/Reference/Global_Objects/String/trimStart
---
<div>{{JSRef}}</div>
<p><strong><code>trimStart()</code></strong>메서드는 문자열 시작부분의 공백을 제거합니다. 또한 <code>trimLeft()</code>라는 별칭으로 호출이 가능합니다.</p>
<div>{{EmbedInteractiveExample("pages/js/string-trimstart.html")}}</div>
<h2 id="syntax">구문</h2>
<pre class="brush: js">
trimStart()
trimLeft()
</pre>
<h3 id="return_value">반환값</h3>
<p><code><var>str</var></code>시작부분(왼쪽)에서 공백이 제거된 새 문자열을 반환합니다.</p>
<p><code><var>str</var></code>에 공백이 없을시에도 에러가 발생하지 않고 여전히 새 문자열(본질적으로 <code><var>str</var></code>의 복사본)이 반환됩니다.</p>
<h3 id="aliasing">별칭</h3>
<p>{{jsxref("String.prototype.padStart")}}표준 메서드 이름과 같은 함수의 일관성을 위해서<code>trimStart</code>가 되었습니다. 그러나,
웹 호환성을 위해서 <code>trimLeft</code> 이라는 별칭을 가집니다. 일부 엔진에서 이것은 다음 예시를 의미합니다.</p>
<pre class="brush: js">String.prototype.trimLeft.name === "trimStart";</pre>
<h2 id="examples">예제</h2>
<h3 id="using_trimStart">trimStart() 사용</h3>
<p>다음 예제는<code>'foo '</code>문자열을 표시합니다.</p>
<pre class="brush: js highlight: [5]">var str = ' foo ';
console.log(str.length); // 8
str = str.trimStart();
console.log(str.length); // 5
console.log(str); // 'foo '
</pre>
<h2 id="polyfill">폴리필</h2>
<pre class="brush: js highlight: [5]">//https://github.com/FabioVergani/js-Polyfill_String-trimStart
(function(w){
var String=w.String, Proto=String.prototype;
(function(o,p){
if(p in o?o[p]?false:true:true){
var r=/^\s+/;
o[p]=o.trimLeft||function(){
return this.replace(r,'')
}
}
})(Proto,'trimStart');
})(window);
/*
ES6:
(w=>{
const String=w.String, Proto=String.prototype;
((o,p)=>{
if(p in o?o[p]?false:true:true){
const r=/^\s+/;
o[p]=o.trimLeft||function(){
return this.replace(r,'')
}
}
})(Proto,'trimStart');
})(window);
*/</pre>
<h2 id="specifications">명세서</h2>
{{Specifications}}
<h2 id="browser_compatibility">브라우저 호환성</h2>
<p>{{Compat}}</p>
<h2 id="See_also">같이 보기</h2>
<ul>
<li><code>String.prototype.trimStart</code>의 폴리필은 <a href="https://github.com/zloirock/core-js#ecmascript-string-and-regexp"><code>core-js</code></a>를 참고하세요.</li>
<li>{{jsxref("String.prototype.trim()")}}</li>
<li>{{jsxref("String.prototype.trimEnd()")}}</li>
</ul>
|