Summary of common methods for strings in JavaScript

Source: Internet
Author: User
Tags array end ming regular expression string split string methods

String is an indispensable element in JavaScript programming, and mastering the common method of string is also the only way in our learning process, we summarize some of the most common string methods.

String.charat (postion)

The Charat method returns the character at the position position in string. Returns an empty string if position is less than 0 or greater than the length of the string. Because JS does not have a character type, the return is still a string

var name= "Deng";
var a=name.charat (1);
document.write (a);

Tring.concat (String ...)

The Concat method joins the other strings together to construct a new string. Consistent with the "+" role

  var name= "Deng"; 
  var a=name.concat ("1", "@2"); 
  document.write (a);

String.IndexOf (searchstring,position)

The IndexOf method finds another string. If it is found, returns the position of the first matching character, or returns-1. optional parameter position can be set to start a lookup from a specified location. For example, to determine whether a label contains a specified class name.

  var classname= "Calss1 class2 class3"; 
  var a=classname.indexof ("Class2"); 
  document.write (a);

String.Replace (Serachvalue,replacevalue)

The Replace method finds and replaces a string and returns a new string. A parameter searchvalue is a string (or a regular expression) that is replaced only where it first appears.

var name= "deng,kun,ming"; 
var a=name.replace (",", "_"); 
  document.write (a);

String.slice (Start,end)

The slice method copies a portion of a string to construct a new string that is equal to the length of End-start.

var name= "deng,kun,ming"; 
  var a=name.slice (2,5); 
  document.write (a);

String.Split (Separtor,limit)

The Split method splits the string into fragments to create an array of strings. Optional parameter limit can limit the number of fragments that are fragmented. If separator is a null character, it returns an array of single characters, which is often used when you need to convert a character to an array.

var name= "dengkunming"; 
  var a=name.split (""); 
  document.write (a);

This method finds all occurrences of separator in string

  var name= "129.168.0.1";   var a=name.split (".");   document.write (a);

String.substring (Start,end)

The substring method gets the substring of the string. function as slice.

var name= "129.168.0.1"; 
  var a=name.substring (2,5); 
  document.write (a);

String.tolowercase ()

The toLowerCase method returns a new string, which is converted to lowercase format. This is often used when judging labels

var name= "DIV SPAN"; 
  var a=name.tolowercase (); 
  document.write (a);



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.