在js中,字符串(string)是基础数据类型中的一种,其中基础数据类型有6种包括(String, Boolean, Number, Undefined, Null, Symbol)
变量声明
// 直接赋值
var name1 = 'this is wuhj!';
// 对象声明式
var name2 = new String('this is wuhj!');
类型检查
var name1 = 'this is wuhj!';
var name2 = new String('this is wuhj!');
console.log(typeof name1); // string
console.log(typeof name2); // object
字符串和其他数据类型的转换
// 字符串转数字
parseInt('123'); // 123
parseInt('123.4'); // 123
parseFloat('123.4'); // 123.4
parseInt('a123bc'); // NaN
parseInt('a123'); // NaN
parseInt('abc'); // NaN
Number('123'); // 123
Number('123aa'); // NaN
//字符串转Boolean
Boolean('abc'); // true
Boolean(''); // false
Boolean('0'); // false, Boolean(Number('0'))
Boolean('1'); // true, Boolean(Number('1'))
// 字符串转数组
'abc'.split(''); // ["a", "b", "c"]
JSON.parse('["a", "b", "c"]'); // ["a", "b", "c"]
//字符串 转对象
JSON.parse('["a", "b", "c"]'); // ["a", "b", "c"]
字符串比较
'a' > 'b'; // false, 转换成ASCII码在进行比较
'1' > '2'; // false, 隐式转换成数字后,再进行比较
'ac' > 'ab'; // true, 第一位(a)ASCII相同后,继续比较下一位(b,c)的ASCII,
'我' > '的'; // false, '我'.charCodeAt() > '的'.charCodeAt(), 25105 > 20340, false
'123' > '我'; // false, 123 > NaN,
//很怪异比较,没有什么意义,但是还是要搞清楚为什么
'abc' > null; // false
'abc' < null; // false
'abc' > undefined; // false
'abc' < undefined; // false
'abc' > true; // false
'abc' < true; // false
'abc' > false; // false
'abc' < false; // false
'abc' > function(){}; // false
'abc' < function(){}; // true
访问方法&字符方法
var str = '123abc';
// 字符串长度
str.length; // 6
// 访问第几个字符
str.charAt(1); // 2
str[1]; // 2
// 访问字符的Unicode编码,返回0到65535之间的整数
str.charCodeAt(0); // 97
位置方法
var str = 'abcabc';
// indexOf
str.indexOf('a'); // 0
str.indexOf('a', 1); // 3
// lastIndexOf
str.lastIndexOf('a'); // 3
去除空格
// 去除收尾空格的兼容写法
function trim(str) {
if (str && typeof str === 'string') {
return str.replace(/^\s+|\s+$/g, '');
}
return str;
}
// 示例
console.log(trim(' Hello World! ')); // 输出: "Hello World!"
// trim
var str = ' abc ';
str.trimLeft(); // 'abc '
str.trimRight(); // ' abc'
str.trim(); // 'abc'
大小写转换
var str = 'aBc';
// toLowerCase 转小写
console.log(str.toLowerCase()); // 'abc'
// toUpperCase 转大写
console.log(str.toUpperCase()); // 'ABC'
操作方法
var str = 'abcdefg';
// 字符串截取之 substr(indexStrar, [strLength])
//提取一个字符串的一部分,并返回一新的字符串
//截取从b开始的3个字符
console.log(str.substr(1, 3)); // 'bcd'
//截取从b开始的所有字符
console.log(str.substr(1)); // 'bcdefg'
// 字符串截取之 substring(indexStart, [indexEnd])
//提取一个字符串的一部分,并返回一新的字符串
//截取从b开始的3个字符
console.log(str.substring(1, 1 + 3)); // 'bcd'
//截取从b开始的所有字符
console.log(str.substring(1)); // 'bcdefg'
//字符串截取之 slice(indexStart, [indexEnd]),
//提取一个字符串的一部分,并返回一新的字符串
//截取从b开始的3个字符
console.log(str.slice(1, 1 + 3)); // 'bcd'
console.log(str.slice(1)); // 'bcdefg'
匹配&替换
var str = 'abcabc';
// 替换到的第一个
console.log(str.replace('a', 'OK了')); // 'OK了bcabc'
//替换全部
console.log(str.replace(/a/g, 'OK了')); // OK了bcOK了bc
// 匹配字符串,返回匹配到的数组结果
console.log(str.match('a')); // ['a']
console.log(str.match(/a/g)); // ['a', 'a']
全局方法 encodeURI() & encodeURIComponent()
两个方法都是对字符串进行编码,并且返回一个新的字符串。区别在于
* encodeURI
* 会替换所有字符,但是不包括 `; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #`
* 通常,get或者post请求中的URL中有`& + =`,但是这些不会被编码,建议使用`encodeURIComponent`
* encodeURIComponent
* 转译除了字母,数字,`( ) . ! ~ * ' - _`之外所有的字符
eval()方法
计算传入的字符串,并且执行其中的js代码。









网友评论