源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript学习笔记(三) String 字符串类型介绍

  • 时间:2020-09-21 20:29 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript学习笔记(三) String 字符串类型介绍
1.字符方法charAt() 、charCodeAt()、fromCharCode()
[u]复制代码[/u] 代码如下:
var stringValue = "hello world"; alert(stringValue.charAt(1));    //"e" alert(stringValue[1]);      //"e" alert(stringValue.charCodeAt(1));  //101 alert(String.fromCharCode(104,101)); //"he"
2.返回子字符串方法slice()、substr()、substring() slice()、substring()方法第一个参数指定子字符串的起始位置,第二个参数指定结算位置(不包括结束位置),原字符串不变 substr()第二个参数指的是返回的字符个数,原字符串不变
[u]复制代码[/u] 代码如下:
var stringValue = "hello world"; alert(stringValue.slice(3)); //"lo world" alert(stringValue.substring(3)); //"lo world" alert(stringValue.substr(3)); //"lo world" alert(stringValue.slice(3,7)); //"lo w" alert(stringValue.subtring(3,7));   //"lo w" alert(stringValue.substr(3,7)); //"lo worl" alert(stringValue.slice(-3)); //"rld",取数组最后3个字符 alert(stringValue.slice(-3)); //"rld",取数组最后3个字符
3.字符串位置方法 indexOf() 和 lastIndexOf() indexOf()方法从前向后搜索子字符串,可接收一个参数或两个参数,第一参数指定要搜索的子字符串,第二个参数指定从该位置向后搜索,没找到返回-1 lastIndexOf()方法从后向前搜索子字符串,可接收一个参数或两个参数,第一参数指定要搜索的子字符串,第二个参数指定从该位置向前搜索,没找到返回-1
[u]复制代码[/u] 代码如下:
var stringValue = "hello world"; alert(stringValue.indexOf("o")); //4 alert(stringValue.lastIndexOf("o")); //7 alert(stringValue.indexOf("o",6)); //7 alert(stringValue.lastIndexOf("o",6)); //4
4.字符串大小写转换方法 toLowerCase()和toUpperCase() toLowerCase()转换为小写,toUpperCase()转换为大写 5.字符串的比较localeCompare() localeCompare()可以比较英文,也可以比较中文,大写字母在前小写字母在后 6.字符串排序:
[u]复制代码[/u] 代码如下:
var stringValue= ["中国","楠楠","俊俊"]; alert(stringValue.sort(stringCompare)); //升序排序函数a-z function stringCompare(value1,value2) { return value1.localeCompare(value2); //降序z-a,value1和value2互换位置 }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部