; String.format
=
function
() {
var
s
=
arguments[
0
];
for
(
var
i
=
0
; i
<
arguments.length
-
1
; i
++
) {
var
reg
=
new
RegExp(
"
\\{
"
+
i
+
"
\\}
"
,
"
gm
"
);
s
=
s.replace(reg, arguments[i
+
1
]);
}
return
s;
};
String.prototype.endsWith
=
function
(suffix) {
return
(
this
.substr(
this
.length
-
suffix.length)
===
suffix);
};
String.prototype.startsWith
=
function
(prefix) {
return
(
this
.substr(
0
, prefix.length)
===
prefix);
};
String.prototype.isPositiveInteger
=
function
() {
return
(
new
RegExp(
/
^[1-9]\d*$
/
).test(
this
));
};
String.prototype.trim
=
function
() {
return
this
.replace(
/
(^\s*)|(\s*$)|\r|\n
/
g,
""
);
};
String.prototype.trimLeft
=
function
() {
return
this
.replace(
/
(^\s*)|\r|\n
/
g,
""
);
};
String.prototype.trimRight
=
function
() {
return
this
.replace(
/
(\s*$)|\r|\n
/
g,
""
);
};
String.prototype.isInteger
=
function
() {
return
(
new
RegExp(
/
^\d+$
/
).test(
this
));
};
String.prototype.isNumber
=
function
(value, element) {
return
(
new
RegExp(
/
^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$
/
).test(
this
));
};
String.prototype.isValidMail
=
function
() {
return
(
new
RegExp(
/
^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$
/
).test(
this
.trim()));
};
String.prototype.isPhone
=
function
() {
return
(
new
RegExp(
/
(^([0-9]{3,4}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0-9]{3,4}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)
/
).test(
this
));
};
String.prototype.isURL
=
function
() {
return
(
new
RegExp(
/
^[a-zA-z]+:\
/
\
/
(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
/
).test(
this
));
};
String.prototype.trans
=
function
() {
return
this
.replace(
/
<
/
g,
'
<
'
).replace(
/
>
/
g,
'
>
'
).replace(
/
"
/
g,
'
"
'
);
};
function
StringBuilder() {
var
sb
=
new
Array();
if
(arguments[
0
]
!=
undefined
&&
arguments[
0
]
!=
null
) {
sb[
0
]
=
arguments[
0
];
}
this
.append
=
function
(str) {
sb[sb.length]
=
str;
};
this
.appendFormat
=
function
() {
var
s
=
arguments[
0
];
for
(
var
i
=
0
; i
<
arguments.length
-
1
; i
++
) {
var
reg
=
new
RegExp(
"
\\{
"
+
i
+
"
\\}
"
,
"
gm
"
);
s
=
s.replace(reg, arguments[i
+
1
]);
}
sb[sb.length]
=
s;
};
this
.toString
=
function
() {
/*
var s = "";
for (var i = 0; i < sb.length; i++) {
s += sb[i];
}
return s;
*/
if
(arguments[
0
]
!=
undefined
&&
arguments[
0
]
!=
null
) {
return
sb.join(arguments[
0
]);
}
return
sb.join(
""
);
};
this
.replace
=
function
(index, str) {
sb[index]
=
str;
};
this
.replaceFormat
=
function
() {
var
s
=
arguments[
1
];
for
(
var
i
=
0
; i
<
arguments.length
-
2
; i
++
) {
var
reg
=
new
RegExp(
"
\\{
"
+
i
+
"
\\}
"
,
"
gm
"
);
s
=
s.replace(reg, arguments[i
+
2
]);
}
this
.replace(arguments[
0
], s);
};
this
.remove
=
function
(index) {
for
(
var
i
=
index
+
1
; i
<
sb.length; i
++
) {
sb[i
-
1
]
=
sb[i];
}
sb.length
=
sb.length
-
1
;
};
this
.insert
=
function
(index, str) {
var
len
=
sb.length
+
1
;
for
(
var
i
=
index; i
<
len; i
++
) {
sb[i
+
1
]
=
sb[i];
}
sb[index]
=
str;
};
this
.insertFormat
=
function
() {
var
s
=
arguments[
1
];
for
(
var
i
=
0
; i
<
arguments.length
-
2
; i
++
) {
var
reg
=
new
RegExp(
"
\\{
"
+
i
+
"
\\}
"
,
"
gm
"
);
s
=
s.replace(reg, arguments[i
+
2
]);
}
this
.insert(arguments[
0
], s);
};
this
.length
=
function
() {
return
sb.length;
};
this
.appendLine
=
function
() {
if
(arguments[
0
]
!=
undefined
&&
arguments[
0
]
!=
null
) {
this
.append(arguments[
0
]);
}
else
{
this
.append(
"
\r\n
"
);
}
}
};
/*
TODO:test utils
var sb = new StringBuilder(true);
sb.append("123");
sb.appendFormat("{0}+{1}", 22, "22");
sb.appendFormat("{0}+{1}", 3333, "333");
//alert(sb.toString());
sb.insert(1, " test insert ");
sb.insertFormat(1, "{0}***{1}", 111, "11 ");
sb.replaceFormat(0, "{0}%%%{1}", 000, "00");
alert(sb.toString(" g "));
*/
function
Uri(urlstr) {
var
uri
=
urlstr;
if
(uri
==
undefined
||
uri
==
null
||
uri
==
""
) {
uri
=
window.location.href;
};
this
.Host
=
function
() {
var
r
=
uri.split(
"
?
"
);
if
(r.length
>
0
) {
return
r[
0
];
}
return
""
;
};
this
.searchString
=
function
() {
var
r
=
uri.split(
"
?
"
);
if
(r.length
>
1
) {
return
unescape(r[
1
]);
}
return
""
;
};
this
.Params
=
function
() {
var
search
=
this
.searchString();
if
(search
==
""
)
return
null
;
var
obj
=
new
Array();
var
pair
=
search.split(
"
&
"
);
if
(pair.length
>
0
) {
for
(
var
i
=
0
; i
<
pair.length; i
++
) {
var
pairArr
=
pair[i].split(
"
=
"
);
obj[pairArr[
0
]]
=
pairArr[
1
];
}
}
return
obj;
};
this
.QueryParam
=
function
(key, def) {
var
obj
=
this
.Params();
if
(obj
!=
null
) {
var
value
=
obj[key];
if
(value
!=
undefined
&&
value
!=
null
) {
return
value;
}
}
return
def;
};
}