-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.js
113 lines (102 loc) · 1.92 KB
/
strings.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
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
/**
* Created by skitsanos on 6/4/14.
*/
String.prototype.isNumeric = function ()
{
return !isNaN(this);
};
String.prototype.times = function (n)
{
var s = this, total = "";
while (n > 0)
{
if (n % 2 == 1) total += s;
if (n == 1) break;
s += s;
n = n >> 1;
}
return total;
};
String.prototype.reverse = function ()
{
var s = "";
var i = this.length;
while (i > 0)
{
s += this.substring(i - 1, i);
i--;
}
return s;
};
String.prototype.trim = function ()
{
var result = this.match(/^ *(.*?) *$/);
return (result ? result[1] : this);
};
String.prototype.ltrim = function ()
{
return this.replace(/^\s+/g, "");
};
String.prototype.rtrim = function ()
{
return this.replace(/\s+$/g, "");
};
String.prototype.repeat = function (times)
{
var ret = '';
for (var i = 0; i < times; i++)
{
ret += this;
}
return ret;
};
String.prototype.startsWith = function (str)
{
return (this.indexOf(str) === 0);
};
String.prototype.endsWith = function (str)
{
var reg = new RegExp(str + "$");
return reg.test(this);
};
String.prototype.mid = function (start, len)
{
if (start < 0 || len < 0) return "";
var iEnd, iLen = String(this).length;
if (start + len > iLen)
{
iEnd = iLen;
}
else
{
iEnd = start + len;
}
return String(this).substring(start, iEnd);
};
String.prototype.htmlEntities = function ()
{
return this.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
};
String.prototype.stripTags = function ()
{
return this.replace(/<([^>]+)>/g, '');
};
String.prototype.capitalize = function()
{
return this.replace(/\w+/g, function(a)
{
return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
});
};
String.prototype.replaceAll = function(f, r)
{
return this.replace(new RegExp(f, 'g'), r);
};
/*
Replaces multiple white spaces and tabs with single white space.
usage: "Hello World. ".squeeze();
*/
String.prototype.squeeze = function()
{
return this.replace(/(\t|\s+)/gm, " ");
};