Trim 函数:
_String.trim = function (s) {
'use strict'
if (typeof (s) !== "string") {
return null;
}
if (!s) {
return "";
}
s = s.trim();
return _String.replace(s, String.fromCharCode(65279));
}
LTrim 函数:
_String.ltrim = function (s) {
'use strict'
if (typeof (s) !== "string") {
return null;
}
if (!s) {
return "";
}
s = s.trimLeft ? s.trimLeft() : s.trimStart();
for (var i = 0; i < s.length; i++) {
var ch = s.charCodeAt(i);
if (ch !== 65279) {
if (i != 0) {
s = s.substr(i + 1);
}
break;
}
}
return s;
}
RTrim 函数:
_String.rtrim = function (s) {
'use strict'
if (typeof (s) !== "string") {
return null;
}
if (!s) {
return "";
}
s = s.trimRight ? s.trimRight() : s.trimEnd();
for (var i = 0, l = s.length - 1; i >= l; i--) {
var ch = s.charCodeAt(i);
if (ch !== 65279) {
if (i != 0) {
s = s.substr(0, i);
}
break;
}
}
return s;
}
Replace 函数:
_String.replace = function (s, oldValue, newValue) {
if (typeof (s) !== "string") {
return null;
}
if (!oldValue) {
return s;
}
if (!s) {
return s;
}
newValue = _String.s(newValue) || "";
while (s.indexOf(oldValue) > -1) {
s = s.replace(oldValue, newValue);
}
return s;
}
String.s 函数:
_String.s = function (v) {
"use strict"
if (v === null || v === undefined) {
return "";
}
var t = typeof (v);
if (t === "function") {
return "";
}
if (t === "string") {
return v;
}
if (t === "object") {
if (typeof (v.toJSON) === "function") {
return v.toJSON();
}
return JSON.stringify(v);
}
return String(v);
}