Home > Web Front-end > JS Tutorial > body text

How to operate arrays and strings in js

小云云
Release: 2018-03-31 17:13:38
Original
4057 people have browsed it

This article mainly shares with you the operation methods of arrays and strings in js, mainly in the form of text and code. I hope it can help everyone.

1. Array operation method

// 1.数组的操作方法
var a = [];
a.unshift()		/*在数组的开头添加一个或者多个元素,返回新长度;IE9+*/
a.shift()		/*删除数组中的第一个元素,返回删除的元素*/
a.push()		/*往数组的末尾添加一个或多个元素,返回新长度*/
a.pop()			/*删除并返回数组的最后一个元素*/

a.slice('start', 'end')	/*不修改原数组,返回一个新数组*/
a.concat()		/*不修改原数组,返回一个新数组。不传参数时,复制数组;传参数时连接数组,可以有多个数组参数;*/
a.splice()		/*删除数组:两个参数,第一个是开始的位置,第二个是删除的长度
				插入元素:至少3个参数,第一个是开始的位置,第二个是0,后面是要插入的元素(可以有多个)
				替换元素:至少3个参数,第一个是开的位置,第二个是替换的个数,后面是要替换的元素*/
a.reverse()		/*反转数组*/
a.sort()		/*用特定的方法对数组进行排列,接收一个排序函数,该函数接收两个参数,
				如果第一个参数在前面,返回一个负数;如果第二个参数在前面,返回一个正数;如果两个参数相等,返回0*/
a.indexOf()		/*在数组中查找元素,两个参数,第一个参数时要查找的元素;第二个参数是要开始查找的位置(该参数可以没有)*/
a.lastIndexOf()		/*从数组的后面开始查找元素,与indexOf相同,只是查找方向不同*/
a.join()		/*把数组格式化成字符串,接收一个参数,即:分隔符。如果不传参数或传入undefined,则以逗号分隔*/
a.toString()		/*返回以逗号为分隔符的数组中的元素组成的字符串*/

a instanceof Array	/*检测是否为数组,如果是,返回true*/
Array.isArray(a)	/*检测是否为数组,如果是,返回true。IE9+*/
Copy after login

2. String operation method

// 字符串操作方法
var b = '54545';
b.concat()			/*拼接字符串,返回新的字符串,不改变原字符串。实践中最常用的是+操作符*/
b.charAt()			/*接收一个参数(字符串中的位置),返回字符串中某位置上的字符*/
b.charCodeAt()			/*与charAt用法相同,返回的是该字符的字符编码*/
String.fromCharCode()	        /*传入字符串的编码,把编码解析为字符串并返回该字符串。可接收多个编码参数*/
b.slice('start', 'end')		/*返回一个新的字符串,不不包含end,不改变原字符串*/
b.substring('start', 'end')	/*与slice一样*/
b.substr('start', 'length')	/*返回一个新字符串,不改变原字符串*/
b.indexOf()			/*在字符串中从前往后查找字符,没找到就返回-1。接收两个参数:第一个参数时要查找的字符;第二个参数是(可选的)开始查找的位置*/
b.lastIndexOf()			/*与indexOf用法相同,改方法是从后往前查找*/
b.search()			/*在字符串中查找,并返回位置。接收一个参数(要查找的字符串),如果没找到,返回-1.从左向右查找*/
b.trim()			/*创建一个字符串副本,删除前置及后缀的所有空格,返回结果。 IE9+*/
b.toLocaleLowerCase()	        /*字符串大写转为小写(针对地区特定的方法,推荐使用)*/
b.toLocaleUpperCase()	        /*字符串小写转为大写(针对地区特定的方法,推荐使用)*/
b.toLowerCase()			/*字符串大写转为小写*/
b.toUpperCase()			/*字符串小写转为大写*/
b.localeCompare('str')	        /*比较字符串b与str,并返回0或一个正数或一个负数。如果在字母表中b在str的前面,就返回一个负数;在后面,返回一个正数;相等返回0*/
b.replace()			/*查找并替换(不改变原字符串),返回一个新字符串,接收两个参数。第一个参数:要替换(查找)的字符串;第二个:要替换成的字符串。*/
// 例子:
function replac() {
	var text = "cat,bat,sat,fat";
	var result1 = text.replace('at', 'ond');				/*第一个参数是字符串*/
	console.log(result1);	    /*"cond,bat,sat,fat"*/			/*只替换第一个*/
	var result2 = text.replace(/at/g, 'ond');				/*第一个参数是正则表达式*/
	console.log(result2);	    /*"cond,bond,sond,fond"*/		        /*全部替换*/
	console.log(text);
};
replac();
Copy after login

Related recommendations:

JS interception and splitting characters Common methods for strings

JS string to remove duplicate characters

js implements string conversion to date format

The above is the detailed content of How to operate arrays and strings in js. 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
Popular Tutorials
More>
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!