forked from ozzcelikk/jquery-string-methods
/
string_extension.js
104 lines (78 loc) · 2.62 KB
/
string_extension.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
// jQuery controls, inspired by c# string object methods : v0.1 (2017/09/0 13:15:43)
var string = {
IsNullOrEmpty: function (parameter) {
if (!parameter || (parameter.toString()
.trim()
.replace(' ', '')
.replace('null', ''))
.length === 0) return true;
return false;
},
Empty: function () {
return "";
},
Format: function (text, parameters) {
var rt = "";
$.each(parameters, function (i, e) {
var p = "{" + i + "}",
t = e.toString(),
c = text.split(p).length - 1;
for (var j = 0; j < c; j++) {
rt = text.replace(p, t),
text = rt;
}
});
return rt;
},
Replace: function (text, key, value) {
var rt = "";
var isExist = text.indexOf(key);
if (isExist > -1) {
var split = text.split(key);
$.each(split, function (i, e) {
if (i < split.length - 1) {
rt += e + value;
} else {
rt += e;
}
});
} else {
return text;
}
return rt;
}
}
/* ----> string.IsNullOrEmpty <----
var text = "";
if(string.IsNullOrEmpty(text) == true){
console.log("object is empty");
}
----> END <---- */
/* ----> string.Empty <----
var text = string.Empty();
console.log(text);
Output => "";
----> END <---- */
/* ----> string.Format <----
var object = {
Id:123,
Name:"test",
ParentId:456
}
---1---
var text = '<label class="label-control" id="{0}" name="{1}" data-parent-id="{2}"';
var tempText = string.Format(text,[object.Id,object.Name,object.ParentId]);
console.log(tempText);
Output => <label class="label-control" id="123" name="Name" data-parent-id="456"
---2--- Use Same Key
var text = '<label class="label-control" id="{0}" name="label_{0}" data-parent-id="{1}"';
var tempText = string.Format(text,[object.Id,object.ParentId]);
console.log(tempText);
Output => <label class="label-control" id="123" name="label_123" data-parent-id="456"
----> END <---- */
/* ----> string.Replace <----
var text = 'JavaScript is a programming language that allows you to implement complex things on web pages, use javascript';
var tempText = string.Replace(text,"JavaScript","whatever")
console.log(tempText);
Output => 'whatever is a programming language that allows you to implement complex things on web pages, use javascript';
----> END <---- */