1.验证字符串是否以某一子串结尾
function endWith (value,str) { if (str == null || str == "" || value.length == 0 || str.length > value.length) return false; if (value.substring(value.length - str.length) == str) return true; else return false; return true; };
2.验证字符串是否以某一子串开始
function startWith (value,str) { if (str == null || str == "" || value.length == 0 || str.length > value.length) return false; if (value.substr(0, str.length) == str) return true; else return false; return true; };
3.计算字符串长度,中文为2,英文为1,以此累计
function charLength (str) { var totalLength = 0; var charCode; for (i = 0; i < str.length; i++) { charCode = str.charCodeAt(i); if (charCode > 256) { totalLength += 2; } else { totalLength++; } } return totalLength; };
(1)计算中文长度
function chineseLength(str){ var totalLength = 0; var charCode; for (i = 0; i < str.length; i++) { charCode = str.charCodeAt(i); if (charCode > 256) { totalLength += 2; } } return totalLength; }
(2)计算英文长度
function englishLength(str){ var totalLength = 0; var charCode; for (i = 0; i < str.length; i++) { charCode = str.charCodeAt(i); if (charCode <= 256) { totalLength++; } } return totalLength; }