JavaScript字符串
-
什么是字符串?
JavaScript 字符串用于存储和处理文本。比如:一句话
,123
,a
-
字符串的创建方式
通过字面量
<script >
var a = "123"
var b ="true"
console.log(typeof a )
console.log(typeof b )
</script>
通过new string的方式
<script >
var a = "123"
var b ="true"
console.log(typeof a )
console.log(typeof b )
var c = new String("hello")
console.log(typeof c)
</script>
字符串的length的属性
返回字符串的长度,中间空格也会被计算。
<script >
var a = "hello 123"
console.log(a.length)
</script>
chartAt
返回到指定的位置,开始的位置是0
<script >
var a = "hello 123"
console.log(a.charAt(4))
</script>
chartCodeAt
返回指定位置上Unicode值
<script >
var a = "hello 123"
console.log(a.charCodeAt(4))
</script>
concat
连接两个或多个字符串,返回连接后的字符串
<script >
var a = "hello 123"
var b ="!!!"
var c = a.concat(b)
console.log(c)
</script>
formCharCode
将 Unicode 转换为字符串
<script >
console.log(String.fromCharCode(111))
</script>
indexOf 和lastIndexOf
indexOf 返回指定字符在字符串中第一次出现的位置,找不到返回-1
lastIndexOf 返回指定字符在字符串中最后一次出现的位置,找不到返回-1
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021071615384890.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NjbHlt,size_16,color_FFFFFF,t_70)
localeCompare
.localeCompare 用本地特定的方式比较两个字符串的顺序,相同返回0,不同返回-1
```javascript
<script >
var a = "hello world"
var b = "hello world"
console.log(a.localeCompare(b))
</script>
<script>
var a = 'hello'
var b = 'world'
console.log(a.localeCompare(b))
</script>
replace
找到指定字符,并替换
<script>
var a ="hello world"
var b =a.replace("hello","")
console.log(b)
</script>
search
检索与正则表达式相匹配的值,能够检索到0,检索不到-1
<script>
var a ="hello world"
console.log(a.search("hello"))
</script>
<script>
var a ="hello world"
console.log(a.search("heun"))
</script>
slice
剪切:以指定的方式将字符串进行切割
<script>
var a ="hello world"
console.log(a.slice("h",3))
</script>
split
提取字符串的片断,并在新的字符串中返回被提取的部分
<script>
var a ="hello world"
var b =a.split("w")
console.log(b)
</script>
<script>
var a ="hello world"
var b =a.split("w")
console.log(b)
var c = "mima@34555"
var d = c.split("@")
console.log("账号:"+ d[0],"密码:"+ d[1])
</script>
substr
通过索引值和数量,返回对应的字符串
<script>
var a ="hello world"
var b = a.substr(a.indexOf("l"),5)
console.log(b)
</script>
substring
通过两个索引值,返回索引值之间的字符串,但不包含第二个索引值位置上的数
<script>
var a ="hello world"
var indexA = a.indexOf("l")
var indexB = a.lastIndexOf("l")
var c = a.substring(indexA,indexB)
console.log(c)
</script>
.toLocaleLowerCase和toLowerCase 将字符串全部转化为小写字母
.toLocaleUpperCase和toUpperCase 将字符串全部转化为大写字母
<script>
var a ="HgHokMN"
var c = a.toLocaleLowerCase()
console.log(c)
var d =a.toLocaleUpperCase()
console.log(d)
</script>
.toString
可以将通过new String方式创建的字符串对象转化为字符串
<script>
var a = new String("hello,world")
console.log(a)
console.log(a.toString())
</script>
valueOf以将通过new String方式创建的字符串对象转化为字符串
<script>
var a = new String("hello,world")
console.log(a.valueOf())
</script>
trim
去除字符串前后空格
<script>
var a = " hello,world "
console.log(a.length)
var b = a.trim()
console.log(b.length)
</script>
算术运算符 + - * / % ++ –
单独存在直接自增或者自减一
<script>
console.log(15+20)
console.log(8-20)
console.log(17*20)
console.log(15/20)
console.log(2%2)
var a =1
a++
console.log(a)
++a
console.log(a)
var b = 1
console.log(b++)
console.log(b)
var c = 1
console.log(++c)
var d = 1
--d
console.log(d)
d--
console.log(d)
console.log(d--)
console.log(--d)
</script>
.三元运算符(三目运算符)
真就执行语句一,假就执行语句二
判断条件:语句一?语句二?
<script>
var a = 6>2? '真':'假'
console.log(a)
</script>
求奇偶数
<body>
<input type="text" id="nnu" />
<button type="button" id="dao">求奇偶数</button>
</body>
<script>
var nnu = document.getElementById("nnu")
var dao = document.getElementById("dao")
dao.onclick =function(){
var nnuValue = nnu.value
var c = nnuValue%2==0 ? '偶数':'奇数'
alert(nnuValue+'是:'+c)
}
</script>
<script type="text/javascript">
var a = 10
var b = 8
var c = a--
console.log(c)
console.log(a--)
console.log(b++)
c = b--
console.log(a,b,c)
</script>
赋值运算符 = += -= *= /= %=
<script type="text/javascript">
var a = 20
var b = 20
var c = 20
var e = 20
a += 5
b *= 5
c /= 5
e %= 5
console.log(a, b, c, e)
</script>
<script type="text/javascript">
var a = 15 == 15
console.log(a)
var b = 25 == 25
console.log(b)
var c = 15 === 15
console.log(c) // true
var d = 25 === '25'
console.log(d) // false
var e = 5!=5
console.log(e)
var f = 25!=`25`
console.log(f)
console.log(1>5)
console.log(1<5)
console.log(15>=15)
console.log(19<=19)
</script>
短路与【逻辑与】 当两个都为true,则结果为true,如果有一个为false,则结果为false
<script type="text/javascript">
var a =8>5
var b = 8<5
var c = a&&b // a=true b false
console.log(c) //false
var d = 10>3
var e = a&&d // a=true d =true
console.log(e) // true
var f = c&&b
console.log(f)
</script>
短路或(逻辑或)如果有一个表达式的结果为true,则结果为true,两个都为false,则结果才为false
<script type="text/javascript">
var n = true
var m = false
console.log(n || m)
</script>
!逻辑非,给boolean类型的值或者表达式取反
<script type="text/javascript">
var d = true
var c = !d
console.log(c)
</script>