Home  >  Article  >  Web Front-end  >  Example analysis of common methods of String in Javascript_javascript skills

Example analysis of common methods of String in Javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:55:051061browse

The examples in this article describe the common methods of String in Javascript. Share it with everyone for your reference. The details are as follows:

// length属性:获取字符串的字符个数。
  var s='爱像一阵风';
  alert(s.length);
// charAt(index)方法:获取指定索引位置的字符,索引从0开始
  var s1='我不要再想你';
  alert(s1.charAt(4));//想
// indexOf('e',startIndex)方法:获取指定字符串第一次出现的位置。startIndex表示从第几个开始搜索。
  var s2='后知过后觉又过了一个秋';
  alert(s2.indexOf('过',3));//6
// split('分隔符',limit);根据分隔符将一个字符串返回为一个数组。
//limit表示要返回的数组的最大长度(可自定义)。
  var s3='快马|在江湖里|厮杀';
  alert(s3.split('|',2));
//以|为分隔,将字符串劈开,显示为数组,且只包含两个元素
  var arr=s3.split('|',3);//三个元素
  for(var i=0;i<arr.length;i++){
  alert(arr[i]);
  }
// substr(startIndex,len)从startIndex开始(包括),往后截取len个字符。
  var s4='一件黑色毛衣';
  alert(s4.substr(4,2));//毛衣,从下标4开始包括4,往后截取两个字符
// substring(startIndex,stopIndex)从startIndex开始,
// 截取到stopIndex位置(不包括stopIndex所在的字符)
  var s5='烟花易冷人事易分';
  alert(s5.substring(1,4));//花易冷,从1开始到4,不包括1和4
// toUpperCase()转换大写、toLowerCase();转换小写
  var s6='What are you kidding me';
  alert(s6.toUpperCase());
  alert(s6.toLowerCase());

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn