栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

每天一篇javascript学习小结(String对象)

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

每天一篇javascript学习小结(String对象)

1、string对象中可以传正则的函数介绍


 var text = "cat, bat, sat, fat"; 
 var pattern = /.at/;
 
 var matches = text.match(pattern); 
 alert(matches.index); //0
 alert(matches[0]);  //"cat"
 alert(pattern.lastIndex); //0
 
 var pos = text.search(/at/);
 alert(pos); //1
 
 
 var result = text.replace("at", "ond");
 alert(result); //"cond, bat, sat, fat"

 result = text.replace(/at/g, "ond");
 alert(result); //"cond, bond, sond, fond"

 result = text.replace(/(.at)/g, "word ($1)");
 alert(result); //word (cat), word (bat), word (sat), word (fat)
 
 function htmlEscape(text){
  return text.replace(/[<>"&]/g, function(match, pos, originalText){
  switch(match){
   case "<":
   return "<";
   case ">":
   return ">";
   case "&":
   return "&";
   case """:
   return """;
  }  
  });
 }
 
 alert(htmlEscape("

Hello world!

")); //<p class="greeting">Hello world!</p> var colorText = "red,blue,green,yellow"; var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"] var colors2 = colorText.split(",", 2); //["red", "blue"] var colors3 = colorText.split(/[^,]+/); //["", ",", ",", ",", ""]

2、字符串转成小写和大写函数

 var stringValue = "hello world";
 alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD"
 alert(stringValue.toUpperCase()); //"HELLO WORLD"
 alert(stringValue.toLocaleLowerCase()); //"hello world"
 alert(stringValue.toLowerCase()); //"hello world"

3、字符串对象 new String()

 var stringObject = new String("hello world");
 var stringValue = "hello world";
 
 alert(typeof stringObject); //"object"
 alert(typeof stringValue); //"string"
 alert(stringObject instanceof String); //true
 alert(stringValue instanceof String); //false

4、字符串fromCharCode()方法


 alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"

5、字符串本地比较方法localeCompare()


 var stringValue = "yellow"; 
 alert(stringValue.localeCompare("brick")); //1
 alert(stringValue.localeCompare("yellow")); //0
 alert(stringValue.localeCompare("zoo")); //-1
 
 //preferred technique for using localeCompare()
 function determineOrder(value) {
  var result = stringValue.localeCompare(value);
  if (result < 0){
  alert("The string 'yellow' comes before the string '" + value + "'.");
  } else if (result > 0) {
  alert("The string 'yellow' comes after the string '" + value + "'.");
  } else {
  alert("The string 'yellow' is equal to the string '" + value + "'.");
  }
 }
 
 determineOrder("brick");
 determineOrder("yellow");
 determineOrder("zoo");

6、indexOf() 和 lastIndexOf()方法


 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 

7、字符串常用字符截取方法


 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.substring(3,7)); //"lo w"
 alert(stringValue.substr(3, 7)); //"lo worl"
 
 alert(stringValue.slice(-3));  //"rld"
 alert(stringValue.substring(-3)); //"hello world"
 alert(stringValue.substr(-3)); //"rld"
 alert(stringValue.slice(3, -4)); //"lo w"
 alert(stringValue.substring(3, -4)); //"hel"
 alert(stringValue.substr(3, -4)); //"" (empty string)

以上就是今天的javascript学习小结,之后每天还会继续更新,希望大家继续关注。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/99888.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号