Java Script 字符串操作

JS中常用几种字符串操作:

  • big()
  • small()
  • bold()
  • fontcolor()
  • fontsize()
  • italics()
  • strike()
  • link()
  • charAt()
  • charCodeAt()
  • concat()
  • fromCharCode()
  • indexOf()
  • lastIndexOf()
  • split()
  • substr()
  • substring()
  • toLowerCase()
  • toUpperCase()

1.字符串显示操作方法:

var str='hello world!';

str.big();
//用大号字体显示字符串;

str.small();
//使用小字号来显示字符串;

str.bold();
//使用粗体显示字符串;

str.strike();
//使用删除线来显示字符串;

str.italics()
//使用斜体显示字符串;

str.fontsize(size);
//使用指定的尺寸来显示字符串;

str.fontcolor(color);
//使用指定的颜色来显示字符串;

str.link(nrl);
//将字符串显示为链接;

 2.charAt();charCodeAt()和fromCharCode()用法:

var str='Hello World!';

str.charAt(1);
//返回指定位置的字符;
//返回字符为:e ;

str.charCodeAt(1);
//返回指定位置的字符的 Unicode 编码;
//返回字符编码为:101 ;

String.fromCharCode(72,69,76,76,79);
//从字符编码创建一个字符串;
//所创建字符串为:HELLO  ;

 3.concat()用法:

var str1="Hello ";
var str2="world!";

str1.concat(str2);
//用于连接两个或多个字符串,返回新的字符串;
//新的字符串为:Hello world! ;

 4.indexOf()和lastIndexOf()用法:

var str="Hello world!";

str.indexOf("Hello");
//返回:0 ;
str.indexOf("World");
//返回:-1 ;
str.indexOf("world");
//返回:6 ;

//该方法可返回某个指定的字符串值在字符串中首次出现的位置;

str.lastIndexOf("Hello");
//返回:0 ;
str.lastIndexOf("World");
//返回:-1 ;
str.lastIndexOf("world");
//返回:6 ;

//该方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索;

//indexOf() 方法对大小写敏感!

 5.split()和substr();substring()用法:

var str="How are you doing today?";
str.split(" ");
//返回值为:['How','are','you','doing','today'] ;
str.split("");
//返回值为:['H','o','w',' ','a','r','e',' ','y','o','u',' ','d','o','i','n','g',' ','t','o','d','a','y','?'] ;
str.split(" ",3);    //以空格分割字符串,返回数组最大长度为3;
//返回值为:['How','are','you'] ;

//该方法用于把一个字符串分割成字符串数组,返回数组;

var str="Hello world!";
str.substr(3,7);    //3为开始位置,7为长度;
//返回值为:'lo worl' ;

//该方法可在字符串中抽取从 start 下标开始的指定数目的字符;

var str="Hello world!";
str.substring(3,7);     //3为开始位置,7为停止位置;
//返回值为:'lo w' ;

//该方法用于提取字符串中介于两个指定下标之间的字符;

 6.toLowerCase()和toUpperCase()用法:

var str="Hello World!";

str.toLowerCase();
//返回值为:'hello world!' ;
//该方法用于把字符串转换为小写,返回一个新的字符串;

str.toUpperCase();
//返回值为:'HELLO WORLD!' ;
//该方法用于把字符串转换为大写,返回一个新的字符串;

转载自本人ITeye链接:http://xiaozhuang0706.iteye.com/blog/2253578

don't look back boy~
原文地址:https://www.cnblogs.com/BHfeimao/p/6495990.html