-
Notifications
You must be signed in to change notification settings - Fork 15
/
jstring.js
64 lines (54 loc) · 1.6 KB
/
jstring.js
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
// ( 1 ) String.contains()
String.prototype.contains = function(text){
if(text == '') return true;
else if(text == null) return false;
else return this.indexOf(text) !== -1;
}
// ( 2 ) String.count()
String.prototype.count = function(text){
if(this.contains(text))
return this.split(text).length-1;
else
return 0;
}
// ( 3 ) String.capitalize()
String.prototype.capitalize = function(){
if(this == '') return this;
else return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
}
// ( 4 ) String.trim()
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g, '');
}
// ( 5 ) String.leftTrim()
String.prototype.leftTrim =function(){
return this.replace(/^\s+/,'');
}
// ( 6 ) String.rightTrim()
String.prototype.rightTrim=function(){
return this.replace(/\s+$/,'');
}
// ( 7 ) String.clear()
String.prototype.clear = function(){
return this.replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
}
// ( 8 ) String.startsWith()
String.prototype.startsWith = function(start) {
if(start == '') return true;
else if(start == null || start.length > this.length) return false;
else return this.substring(0,start.length) == start;
}
// ( 9 ) String.endsWith()
String.prototype.endsWith = function(end) {
if(end == '') return true;
else if(end == null || end.length > this.length) return false;
else return this.indexOf(end, this.length - end.length) !== -1;
}
// ( 10 ) String.insert()
String.prototype.insert = function(text,at) {
if(at == null || at > this.length)
at = this.length;
else if(at < 0)
at = 0;
return this.substring(0,at)+text+this.substring(at);
}