美文网首页
字符串定义和基本方法介绍

字符串定义和基本方法介绍

作者: 奔云 | 来源:发表于2019-11-14 19:30 被阅读0次

第一部分:

字符串定义: 它是编程语言中的一种基本数据类型,它由数字、字母、下划线组成的一串字符,通常以串联的整体作为操作对象.
  • 定义扩展延伸:

组成的字符可以是 零个 或 多个, 放在 单引号 或者 双引号之间. 如果字符串里面有单引号子内容,则整个字符串就要用 双引号.(如果非要单引号里面使用单引号,那就必须使用 反斜杠 \ 来把引号转义掉)

//双/单引号
var str3 = 'hel'l'o world'
console.log(str3) //  "error"

var str4 = "hel'l'o world"
console.log(str4)  // "hel'l'o world"

// 反斜杠转义
var str5 = "hel'l'o world"
var str6 = 'a\'b\'c'

console.log(str5)  //"hel'l'o world"
console.log(str6)  // "a'b'c"

空格也会占用空间,影响字符串的长度. 如下:

var str1 = 'hello world'
var str2 = 'hello w o rld'
console.log(str1.length) // 11
console.log(str2.length) // 13

字符串格式形式一般都是在同一行内, 如果非要在多行来表示一个字符串,就要用到
①空格+反斜杠 (不推荐)
②使用 + 符号来连接 (推荐)

var str7 = 'hello \
world \
ni \
hao';
console.log(str7) // "hello world ni hao" 


var str8 = 'hello'
+ ' world'
+ ' ni'
+ ' hao';
console.log(str8) // "hello world ni hao"

字符串多行显示方法,使用 `(数字1左边键)小斜点, 这是ES6新增功能

var str9 = `hello 
world
ni 
hao
`
console.log(str9) 
               // 输出结果为:
                          "hello 
                           world
                           ni 
                           hao
                            "

 

第二部分:

为了方便记忆,我们把字符串的基本方法先分类: 查 ,增, 删, 改, 转

  • 长度: string.length
var str1 = 'abc 123'
console.log(str1.length) // 6 注意空格也算一个单位长度
  • 通过索引值来查看所在字符 str[num]
var str = 'hello world '
console.log(str[3]) // "l"

// 注意 这个方法 不能实现 通过赋值来改变 字符串 ,如下:
var str = 'hello world '
str[3] = 'w'
console.log(str) //   "hello world " 原数组不改变, 赋值无效
  • 通过索引值查来找字符: string[num] ; string.charAt(num)
var str = 'hello world'
var str1 = str[2]
var str2 = str.charAt(4)
var str3 = str[20]  // 询查不到返回 undefined 
var str4 = str.charAt(20) // 询查不到返回 "" 

console.log(str1) // "o"
console.log(str2) // "o"
console.log(str3) // undefined 
console.log(str4) // ""
  • 通过字符来查找索引值: string.search('char') ; string.indexOf('char') ; string.lastIndexOf('char')
var str = 'hello world'
var search = str.search('l')
var search1 = str.search('a') // 查询不到 返回 -1
var index = str.indexOf('o')
var index1 = str.indexOf('a')  // 查询不到 返回 -1
var lastIndex = str.lastIndexOf('l')

console.log(search) //2
console.log(search1) //-1
console.log(index)  //4
console.log(index1)  //-1
console.log(lastIndex)  //9
  • string.match('chars') 匹配原数组chars,如果存在,以数组返回chars,不存在 返回 null
var str = 'hello world '
var matchStr = str.match('el')
console.log(matchStr) // ["el"]

var matchStr1 = str.match('le')
console.log(matchStr1) // null

  • 遍历一个字符串,是ES6 新增: for ( .. of ..)
var str = 'hello world'
for (var i of str){
  console.log(i)
}
// "h" "e" "l" "o" " " "w" "o" "r" "l" "d"

// 或者 封装成函数
function ergStr(string){
  for (var i of string){
    console.log(i)
  }
}
ergStr(str) // "h" "e" "l" "o" " " "w" "o" "r" "l" "d"
  • 查看某字符出现的次数 函数 countchar(str,c) :
var str = ' hellw world'
function countchar(str,c){
  count = 0
  for (var char of str){
    if( char == c){
      ++count 
    }
  }
   console.log(count)
}

countchar(str,'l') // 3

  • ''+'' 运算符来 增添字符, 简便,实用 前后都可添加 :
var str = 'hello world'
var strplus = str+'www'
console.log(strplus) // "hello worldwww"

var plusstr = 'ni hao '+str
console.log(plusstr)  // "ni hao hello world"

  • string.concat('chars') 增添字符
var str = 'hello world'
var strconcat = str.concat('yyy')
console.log(strconcat) // ""hello worldyyy""
  • string.replace('char','replacechars') 这个是一个变相的增添,可以在定位的字符后面来添加字符串
var str = 'hello world '
var strPlaceAdd = str.replace('lo','lo this')
console.log(strPlaceAdd) // "hello this world "

删 : 可以删除字符 或 字符段 还是 空格

  • 使用repalce来删除某一个或一段字符, 原字符串不变; 还可以用来删除字符串所有的空格
var str =  'hello world '
var deleteStr = str.replace('e','')
console.log(deleteStr) // " hllo world "

var deleteStr1 = str.replace('ell','')
console.log(deleteStr1) // " ho world "

// 删除所有字符串里所有空格
var deleteSpace = str.replace(/\s*/g,'')
console.log(deleteSpace) // "helloworld"
  • string.trim()删除两边空格,原字符串不变
var str = ' hello world '
var trimStr = str.trim()
console.log(trimStr) // "hello world"

改: 可以引申为 改变,截取,限缩 字符串

  • string.substring(startIndex, endIndex) 截取元素一段字符串, 不改变原数组, 注: 截取不包括endIndex所在字符
var str = 'hello world '
var substring = str.substring(3,8)
console.log(substring)  // "lo wo"  注: 截取不包括endIndex所在字符
  • string.substr(startIndex, length) 从开始索引值截取一段长度的字符串,元字符串不变
var str = 'hello world '
var substr = str.substr(3,8)
console.log(substr)  / "lo world"
  • string.slice(startIndex,endIndex) 截取startIndex 到 endIndex 的字符段, 不包括endIndex所在字符
var str = 'hello world '
var sliceStr = str.slice(3,8)
console.log(sliceStr) // "lo wo"

//注意 slice 允许负数索引值的出现, 表示 倒数的索引值: 
var sliceStr1 = str.slice(3,-2)
console.log(sliceStr1) // "lo worl"
var sliceStr2 = str.slice(-4,-1)
console.log(sliceStr2) // "rld"
  • string.toUpperCase() 和 string.toLowerCase() 全局改变字符成大/小写
var str =  'Hello World '
var upperStr = str.toUpperCase()
var lowerStr = str.toLowerCase()
console.log(upperStr) // "HELLO WORLD "
console.log(lowerStr) // "hello world "

//如果要想改变某个字符的大小写 就用 .replace() 如: 
var reStr = str.replace('lo','LO')
console.log(reStr) // "HelLO World "

字符串与数字,数组,对象的类型转换

  • 转成 数字类型:
    ① parseInt( string ) : 转换为整数, 忽略小数点后面数字 以及 忽略字符串
    ② parseFloat( string) : 转换为浮点型数字,遇到非数字自动舍去,只认识第一个小数点,后面的小数点自动舍去
    ③ Number(string) : 强制转换为数字, 不能有非数字以外类型, 否则返回NaN
var str = '123px'
var floatStr = '123.123px'

// parseInt()
var intNum = parseInt( str )
var intNum1 = parseInt( floatStr )
console.log( intNum )  // 123 
console.log( intNum1 ) // 123

// parseFloat()
var floatNum = parseFloat( floatStr )
console.log( floatNum ) // 123.123

// 注意 parseFloat 只能识别到第一个小数点,如下: 
var floatStr1 = '123.456.789px'
var floatNum1 = parseFloat( floatStr1 )
console.log( floatNum1 ) // 123.456

// Number()
var num = Number(str)
console.log( num ) // NaN

//  数值 转 字符串 
var num = 123456
var numStr1 = num + ''
var numStr2 = num.toString()
console.log(numStr1) // "123456"
console.log(numStr2) // "123456"
  • 转成 数组类型 string.split('separator')
var str = '123,abc,DEF'
var arrStr = str.split(',')
console.log(arrStr) // ["123", "abc", "DEF"]
console.log(str) //  "123,abc,DEF" 不改变原字符串

var arrStr1 = str.split("")
console.log(arrStr1 ) // ["1", "2", "3", ",", "a", "b", "c", ",", "D", "E", "F"]

// 注意 match()方法也是返回一个数组
var matchStr = str.match('3,a')
console.log( matchStr ) // ["3,a"]

// 数组 转换 字符串方法
var arr = ['abc','123','DBC']
var arrStr = arr.join('')
console.log( arrStr ) "abc123DBC"

  • 转成 对象类型 JSON.parse(string)
var str = '{"name": "li", "age": 18, "sex": "male"}'
var objStr = JSON.parse(str)
console.log( objStr ) // { age: 18, name: "li",sex: "male"}

// 对象 转换 字符串方法
var reStr = JSON.stringify( objstr )
console.log( reStr ) // "{"name":"li","age":18,"sex":"male"}"

相关文章

  • 字符串定义和基本方法介绍

    第一部分: 字符串定义: 它是编程语言中的一种基本数据类型,它由数字、字母、下划线组成的一串字符,通常以串联的整体...

  • Levenshtein distance(编辑距离)

    基本介绍 Levenshtein distance是一种度量两个序列(字符串)差异大小的方法。 该方法定义如下:两...

  • Swift字符串

    一、字符串的介绍 OC和Swift中字符串的区别: 使用String的原因: 二、字符的定义 定义不可变字符串 定...

  • 进阶篇/Chapter 4

    PHP字符串介绍 我们之前提到过字符串的三种定义方法:1)单引号2)双引号3)heredoc语法结构 单引号和双引...

  • 指针与数组

    基础 方法一:用字符数组定义字符串,用指针访问。 方法二:用指针定义字符串,用数组下标访问。 C语言没有定义字符串...

  • 自定义View学习总结(二)

    在我们的自定义学习总结的第一篇,介绍了自定义控件的基本流程,几个核心方法和核心api,这篇主要介绍给自定义控件增加...

  • 超详细的数组方法介绍

    本篇将介绍关于数组的方法和方法之间的区别。 一.join() 定义:将数组中所有元素都转化为字符串并连接在一起,返...

  • 【前端面试题】考查数组对象方法和字符串对象方法

    一.数组对象方法和字符串对象方法参考 1.数组对象方法 2字符串对象方法 二. 题目 1.翻转字符串 :定义一个翻...

  • js判断输入是否全是空格

    使用JavaScript trim() 方法 定义和用法 trim() 方法用于删除字符串的头尾空格。(这个方法可...

  • Dart 数据类型及相关方法

    基本数据类型 部分属性 常用方法 ———————————————— String类型 字符串定义 使用单引号或双引...

网友评论

      本文标题:字符串定义和基本方法介绍

      本文链接:https://www.haomeiwen.com/subject/ubcdictx.html