使用charAt()方法截取姓名字符串后两位

定义:

charAt() 方法可返回指定位置的字符。

JavaScript 并没有一种有别于字符串类型的字符数据类型,所以返回的字符是长度为 1 的字符串。

语法:

stringObject.charAt(index)

TIY

<html>
<body>

<script type="text/javascript">

var str="Hello world!"
document.write("The first character is: " + str.charAt(0) + "<br />")
document.write("The second character is: " + str.charAt(1) + "<br />")
document.write("The third character is: " + str.charAt(2))

</script>

</body>
</html>

显示

The first character is: H
The second character is: e
The third character is: l

应用:

因为charAt()方法只能截取一个字符,所以要想获取姓名后两位只能拼接起来。

var applyUserId = data.data.rows[i].userName;//获取姓名
var namesmall = applyUserId.charAt(applyUserId.length - 2) + applyUserId.charAt(applyUserId.length - 1); // 截取姓名后两位

显示:

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章