js String类型方法笔记,用于查询或者CV
目录
length属性
// String类型
// 1. length
// 每个实例对象都有一个length属性
(function () {
var str = "你好!j s";//中文、符号、空格都算一个字符
console.log(str.length);//6
})();
下标找出字符
(function () {
var str = "一点就到家";
console.log(str.charAt(2));//就
console.log(str.charCodeAt(1));//28857
console.log(str[2]);//就
//console.log(str[-1]); 不可以使用负数,但是可以使用长度+负数
console.log(str[str.length - 1]);//家
})();
拼接concat , +
(function () {
// 1. contact方法(很少使用,一般使用 +代替)
var value = "hello";
var value1 = " world";
var value2 = "!";
// concat方法可以拼接多个字符串
var result1 = value.concat(value1, value2);
console.log(value + value1 + value2);//hello world!
console.log(result1);//hello world!
console.log(value);//hello
})();
切片slice=substring,substr
// slice(startIdx,endIdx)方法 slice[slaɪs]切
// substring(startIdx,endIdx)方法 substring子串
// 0 <= Idx < str.length(可选,默认为字符串的长度)
// substr(startIdx,chartNum)方法 chartNum(可选,表示返回字符的个数) substr子字符串
//如果为负数,则会将负值加上字符串的长度
(function () {
var stringValue = "胡小明回家了";
console.log(stringValue.slice(1));//小明回家了
console.log(stringValue.slice(1, 3));//小明
console.log(stringValue.substring(1));//小明回家了
console.log(stringValue.substring(1, 3));//小明
console.log(stringValue.substr(1));//小明回家了
console.log(stringValue.substr(1, 3));//小明回
})();
位置查找下标indexOf
// 查找字符的下标
//indexOf("查找的字符串","起始位置")从前面向后查找
//lastIndexOf("查找的字符串","起始位置")从后向前查找
(function () {
var stringValue = "hello world";
console.log(stringValue.indexOf("lo"));//3
console.log(stringValue.lastIndexOf("o"));//7
console.log(stringValue.indexOf("o", 6));//7
console.log(stringValue.lastIndexOf("o", 6));//4
})();
删除前置及后缀trim
//trim()方法创建一个字符串的副本,删除前置及后缀的所有空格
(function () {
var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
console.log(trimmedStringValue);//hello world
console.log(stringValue);// hello world
})();
大小写转换toLowerCase、toUpperCase
// 字符串大小写转换
(function () {
var stringValue = "hello world"
// toLocaleUpperCase和toLocaleLowerCase针对特定地区的语言
console.log(stringValue.toLocaleUpperCase());
console.log(stringValue.toLocaleLowerCase());
console.log(stringValue.toUpperCase());
console.log(stringValue.toLowerCase());
})();
匹配方法
// 字符串的模式匹配方法
(function () {
var text = "cat, bat, sat, fat";
var pattern = /.at/;
var matches = text.match(pattern);
console.log(matches);//Array(1)
})();
替换
(function () {
var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
console.log(result);//cond, bat, sat, fat
result = text.replace(/at/g, "ond");
console.log(result);//cond, bond, sond, fond
// 匹配模式的子字符串$&
result = text.replace(/.at/g, "word($&)");
console.log(result);//word(cat), word(bat), word(sat), word(fat)
})();
Html的特殊字符的转义
// Html的特殊字符的转义
(function () {
function htmlEscape(text) {
return text.replace(/[<>"&]/g, function (match, pos, originalText) {
switch (match) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case '"':
return """
}
});
}
console.log(htmlEscape("<p class='className'>hello world!</p>"));
})();
分割成数组
//split(分割符号,返回的结果数)方法
//返回数组
(function () {
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");
console.log(colors1);// ["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2);
console.log(colors2);// ["red", "blue"]
var colors3 = colorText.split(/[,.]/);
console.log(colors3);// ["red", "blue", "green", "yellow"]
})();
转成数字
// parseInt
(function () {
var numstr = "1";
var num = parseInt(numstr);
console.log(typeof num);//number
console.log(num);//1
})();