JS验证字符串是否以某一子串结尾,验证字符串是否以某一子串开始;计算字符串长度,中文为2,英文为1,以此累计

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;
}

 

posted on 2016-05-21 10:45  laugher_ccc  阅读(294)  评论(0编辑  收藏  举报

导航