德胜云资讯,添加一些关于程序相关的内容,仅供大家学习交流(https://www.wxclwl.com)
日期:2023/03/27 18:33作者:吴思翰人气:
typescript编程实践
一、简介
概念:由零个或多个字符组成的有限序列,由单引号()或双引号("")来定义。
let str1: string = // 空串 let str2: string = // 空格串 let str3: string = Hello, world. // 字符串定义:
string // 基本类型
String // String类
let text1: string = This is a String. // 由单引号()或双引号("")直接定义 let text2: String = new String(This is a String.) // 由构造函数String()来间接定义二、实例属性
constructor 构造函数的引用
let s1 = new String(This is a String) console.log(s1.constructor) // [Function: String]length 字符串的长度
let s2 = new String(Hello, world.) console.log(s2.length) // 13 空格也算prototype 构造函数原型的引用
let s3 = new String(I am a student.) console.log(s3.prototype) // undefined三、实例方法
charAt() // 返回指定位置的字符
charCodeAt() // 返回指定位置的字符的ASCII码
indexOf() // 返回某特定串在字符串中首次出现的位置
lastIndexOf() // 返回某特定串在字符串中最后出现的位置
toLocaleLowerCase() // 根据本地语言环境把字符串转为小写
toLocaleUpperCase() // 根据本地语言环境把字符串转为大写
toLowerCase() // 把字符串转为小写
toUpperCase() // 把字符串转为大写
substr(m, n) // 截取从m开始到m+n的字符串,m+n不取
substring(m, n) // 截取从m开始到n的字符串,n不取
toString() // 返回字符串
valueOf() // 返回字符串
split(c) // 根据字符c来分割字符串为字符串数组
slice(m, n) // 对字符串切片,返回切片字符串
search(RegExp) // 按正则表达式搜索,返回下标或-1
match(RegExp) // 按正则表达式匹配,返加数组或null
replace(RegExp, new) // 将正则表达式匹配串替换成新串
concat() // 连接两个或多个字符串,并返回新的字符串
let str = I am Chinese. console.log(str.charAt(2)) // a 下标2的字符,从0开始计数 console.log(str.charCodeAt(2)) // 97 字符a的ASCII码 console.log(str.indexOf(am)) // 2 子串am首次出现的下标 console.log(str.lastIndexOf(e)) // 11 子串e最后出现的下标 console.log(str.toUpperCase()) // I AM CHINESE console.log(str.toLowerCase()) // i am chinese console.log(str.toLocaleUpperCase()) // I AM CHINESE console.log(str.toLocaleLowerCase()) // i am chinese console.log(str.substr(2, 6)) // am Chi [2, 2+6) 2+6不取 console.log(str.substring(2, 6)) // am C C [2, 6) 6不取 console.log(str.valueOf()) // I am Chinese console.log(str.toString()) // I am Chinese console.log(str.split()) //[I, am, Chinese] 以空格分割串 console.log(str.slice(0, 6)) // I am C [0, 6) 6不取 console.log(str.search(/ese/gi)) // 9 正则搜索,搜索到返回下标,搜索不到返回-1 console.log(str.match(/hi/gi)) // [hi] 正则匹配,以数组形式返回所有匹配正则表达式的串,无匹配返回null console.log(str.replace(I am, You are)) // You are Chinese. let str1 = Good let str2 = Girl let str3 = str1.concat(str2) console.log(str3) // Good Girl四、字符串拼接
// 使用连接运算符 + let s1 = abc let s2 = 123 let n1 = 1 let n2 = 9 let flag = true console.log(s1 + s2) // abc123 console.log(s1 + n1 + n2) // abc19 console.log(n1 + n2 + s1) // 10abc console.log(s1 + flag) // abctrue // 使用模板字符串 `${}` console.log(`${n1}${n2}${s1}`) // 19abc console.log(`${s1}${flag}`) // abctrue // concat()方法,javascript不限制参数类型,TypeScript仅字符串类型 console.log(s1.concat(flag)) // abctrue console.log(s1.concat(n1)) // abc1五、字符串转换
基本类型转字符串
// toString() let n1: number = 100 let f1: boolean = true let s1: symbol = Symbol() console.log(n1.toString()) // 100 console.log(f1.toString()) // true console.log(s1.toString()) // Symbol() // 连接符 + console.log( + n1) // 100 // 模板字符串 `${}` console.log(`${f1}`) // true字符串转基本类型
// 包装函数Number() Boolean() Symbol() let str: string str = 123 console.log(Number(str)) // 123 str = true console.log(Boolean(str)) // true str = symbol console.log(Symbol(str)) // Symbol(symbol) // parseInt() parseFloat() console.log(parseInt(123456)) // 123456 console.log(parseFloat(123.456)) // 123.456罗马帝国崩溃的一个主要原因是,没有0,他们没有有效的方法表示他们的C程序成功的终止。–Robert Firth