String objects and methods in JavaScript

巴扎黑
Release: 2017-06-26 11:54:37
Original
1233 people have browsed it

string object

Two kinds of creation of string object

var a="hello";

var b=new String("hello");

//The following is the method

//charAt() finds the characters in the string according to the subscript

alert(a. charAt(3));//Return a certain character in the string based on the subscript

alert(a.charAt(10));//Empty, the subscript cannot be found

//charCodeAt() returns the character at the specified position

var a="Hello world!Hello world!";

var d=" This is a string ";

alert(a.charCodeAt(1));//101

alert(d.charCodeAt(3));// 26465

//String.fromCharCode() uses unicode encoding to return a string

alert(String.fromCharCode(65,66,67 ));//

//concat() is used to connect one or more strings without changing the original array

var a="Hello world!Hello world !";

var b="Purple smoke rises from the sunshine incense burner";

var c="Not as good as Wang Lun to send me love";

alert(a.concat(b ,c))

//search() finds the string to be matched. If found, returns the subscript of the first match. If not found, Return -1

var c="123456789";

alert(c.search("567"));//4

alert(c.search("5671"));//-1

//replace() Use some words to replace other characters

var d="Xiao Huang is a dog, Xiao Huang is very handsome";

var e= d.replace("小黄","小黑");//Xiao Huang changes to Xiao Hei, only for the first time

var e=d.replace(/小黄/g,"小黑")//Everything in the string is changed

alert(e);

//split() Used to split a string into a string array, the original array remains unchanged

var a="hello world!";

   alert(a.split(" "));

alert(a.split(("o"));//hell, w,rld

()Returns the position where a specified character first appears in the string

var a="hello world!";

alert( a.indexOf("o"));//4

alert(a.indexOf("p"));//-1 No results were found -1

//lastIndexOf() searches from back to front



var a="hello world!";

 alert(a. lastIndexOf("o"));//7. ##    alert('world')//If you find the first one, you will not find it 5

   alert(a.match(/world/g));//Regular expression (global search)

//slice() can extract a certain part of the string, which can be negative

  var a="Hello world!Hello world!";

  alert(a .slice(5,13));//The first subscript is required, not the second subscript

   alert(a.slice(-15,-7));

//substring() is the same as slice, but does not accept negative numbers

  var a="Hello world!Hello world!";

  alert (a.substring(4,12));

  alert(a.substring(-3,-1));//Nothing

// substr() intercepts the specified number of characters starting from the specified subscript

  var a="Hello world!Hello world!";

  alert(a.substr(4,6)); //Truncate 6

starting from subscript 4 backwards

//toLowerCase() converts the string to lowercase

!";

alert(a.toLowerCase()); !";

alert(a.toUpperCase());

The above is the detailed content of String objects and methods in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!