String objects and array objects in javascript

Posted by mariolopes on Fri, 07 Jun 2019 00:20:34 +0200

1. The concept of JavaScript objects

In javascript, except for null and undefined, other data types are defined as objects

Variables, string, math, array, and data can also be defined by creating objects, which are important built-in objects in javascript.

Most of the functions of javascript programs are object-based

var aa=Number.MAX_VALUE;            //Using Digital Object to Get the Maximum Representable Number
var bb=new String("hello world");   //Create string objects
var cc=new Date();                  //Create a date object
var dd=new Array("monday","tuesday","thirsday","foutday"); //Array object

2. Classification of built-in objects in JavaScript

data object

Number digital object
 String string object
 Boolean Boolean Object

Composite objects

Array array object
 Math mathematical object
 Date Date object

Advanced Objects

Object custom object
 Error error object
 Function function object
 Regexp Regular Expression Object
 Global global object

3. String objects in JavaScript

1. Creation of String Objects

There are two ways to create strings:

1. Variable = "String"
2. String object name = new String (string)

Example:

var str1="hello world";
var str1= new String("hello word");

2. Attributes and functions of string objects

str.length

Gets the length of the string

Example:

var txt="Hello World!";
document.write(txt.length);

Return:

12

str.toLowerCase()

Converting strings to lowercase

Example:

var str="Hello World!";
document.write(str.toLowerCase());

Return:

hello world!

str.toUpperCase()

Convert strings to uppercase

Example:

var str="Hello World!";
document.write(str.toUpperCase());

Return:

HELLO WORLD!

str.trim()

Remove whitespace on both sides of a string

Example:

var str1="    hello world   ";
document.write(str1.length + "<br />");
document.write(str1.trim() + "<br />");
document.write(str1.trim().length);

Return value:

18
hello world
11

str.charAt(index)

Returns the character of the specified index, the subscript of the first string of which is 0

Example:

var str="Hello world!";
document.write(str.charAt(1));

Return:

e

str.indexOf(findstr,index)

Returns the first occurrence of a specified character in a string

Starting from the index of the string str, find findstr, return to the location where findstr first appears if found, and start from scratch if no index is specified. If no string is found, return - 1, case sensitive.

Example:

var str="Hello world!";
document.write(str.indexOf("Hello") + "<br />");
document.write(str.indexOf("World") + "<br />");
document.write(str.indexOf("world") + "<br />");

Return value: 0 -1 6

str.lastIndexOf(findstr,index)

Find the specified character findstr forward at index in the string str. If no index is specified, look backward and forward. If findstr is found, return the position of the first findstr in the string str.

If the specified string is not found, it returns - 1, case sensitive

Example:

var str="Hello world!";
document.write(str.lastIndexOf("Hello") + "<br />");
document.write(str.lastIndexOf("World") + "<br />");
document.write(str.lastIndexOf("world"));

Return:

0
-1
6

str.match(findstr)

Find the specified character in the string, which can be a regular expression

If the specified string is found in the string str, the found string is returned, and if not, null is returned.

Example:

var str="Hello world!";
document.write(str.match("world") + "<br />");
document.write(str.match("World") + "<br />");
document.write(str.match("worlld") + "<br />");
document.write(str.match("world!"));

Return:

world
null
null
world!

str.search(regexp)

Find the specified substring or substring that matches the regular expression in the string str

Returns the starting position of the specified substring in the string str, and - 1 if the substring is not matched, case-sensitive.

Example:

var str="hello world!";
document.write(str.search(/world/));
document.write(str.search(/World/));

Return:

6
-1

str.substr(start,length)

Extracting a string of specified length from the beginning index of the string str

If no length is specified, all characters from start to end are extracted

Example 1:

var str="Hello world!";
document.write(str.substr(3));

Return:

lo world!

Example 2:

var str="Hello world!";
document.write(str.substr(3,7));

Return:

lo worl

str.substring(start,end)

Extract the string between two indexes in the string str, excluding the characters at the end If the start and end values are equal, an empty string is returned

Example 1:

var str="Hello world!";
document.write(str.substring(3));

Return:

lo world!

Example 2:

var str="Hello world!";
document.write(str.substring(3,7));

Return:

lo w

str.slice(start,end)

Slice the str ing and return all the characters from start (including start) to end (excluding end).

Example 1:

var str="Hello happy world!";
document.write(str.slice(6));

Return:

happy world!

Example 2:

var str="Hello happy world!";
document.write(str.slice(6,11));

Return:

happy

str.replace(oldstr,newstr)

Replace the oldstr of str in the string with newstr

Example:

var str="hello world!";
document.write(str.replace(/world/, "javascript"));

Return:

hello javascript!

str.split(sep,num)

The string str is divided into string arrays by sep and num is the maximum number of separable strings.

Example 1:

var str="How are you doing today?";
document.write(str.split(" ") + "<br />");
document.write(str.split("") + "<br />");
document.write(str.split(" ",3));

Return:

How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you

Example 2:

"2:3:4:5".split(":");   //Will return ["2", "3", "4", "5"]
"|a|b|c".split("|");    //Will return ["," "a," "b," "c"]
"hello".split("");  //Return ["h", "e", "l", "l", "o"]
"hello".split("", 3);   //Return ["h", "e", "l"]

str.concat(str1,str2...)

Connect two or more strings

Example:

var str1="Hello ";
var str2="world!";
document.write(str1.concat(str2));

Return:

Hello world!

4. Array in JavaScript

1. Three ways to create arrays:

1. The var array name= [element 1, element 2, element 3...];

For example:

var arr1=[1,2,3,4];

2. The var array name = new Array (element 1, element 2, element 3...)

For example: var arr2=new Array(5,6,7,8);

3. var array name = new Array (array length);

    var array name [0]= "the value of the first element of the array";
    var array name [1]= "the value of the second element of the array";
    var array name [2]= "the value of the third element of the array";

2. Attributes and Methods of Array Objects

arr.join(sep)

Splice all elements in an array into a string using sep. If no separator is specified, use a comma as the separator.

Example 1:

var arr = new Array(3);
arr[0] = "hello";
arr[1] = "python";
arr[2] = "javascript";
document.write(arr.join());

Return:

hello,python,javascript

Example 2:

var arr = new Array(3);
arr[0] = "hello";
arr[1] = "python";
arr[2] = "javascript";
document.write(arr.join("."));

Return:

hello.python.javascript

arr.concat(array1,array2...)

Aray1 can be either a value or an array object. This method returns a new array with its own parameters joined together.

Example 1:

var a = [1,2,3];**
document.write(a.concat(4,5));

Return:

1,2,3,4,5

Example 2:

var arr = new Array(2); arr[0] = "hello"; arr[1] = "python";

var arr2 = new Array(2); arr2[0] = "hello"; arr2[1] = "javascript";

document.write(arr.concat(arr2)); Return:

hello,python,hello,javascript

arr.reverse()

Inverse arrays, and the original arrays will be changed

Example:

var arr = new Array(3);
arr[0] = "hello";
arr[1] = "python";
arr[2] = "javascript";

document.write(arr + "<br />");
document.write(arr.reverse());

Return:

hello,python,javascript
javascript,python,hello

arr.sort()

Sort the elements of an array, and the original array will be changed

Example:

var arr = [11,33,55,77,66,44,22];
document.write(arr.sort());

Return:

11,22,33,44,55,66,77

arr.slice(start,end)

Returns a new array consisting of elements arr from start (including start) to end (excluding end) When end is not specified, it returns an array of all elements from start to end

Example 1:

var arr = [11,33,55,77,66,44,22];
document.write(arr.slice(2,6));

Return:

55,77,66,44**

Example 2:

var arr = [11,33,55,77,66,44,22];
document.write(arr.slice(2));

Return:

55,77,66,44,22

arr.splice(start,deleteCount,value1,value2)

Delete the deleteCount element from the start index of the array arr, add value1,value2 to the location of the deleted element, and return the deleted element. The original array will be changed.

Example:

var arr = [11,33,55,77,66,44,22];
document.write(arr.splice(2,3,88,99)+"<br>");
document.write(arr);

Return:

55,77,66
11,33,88,99,44,22

arr.push(value1,value2,value3)

Add one or more elements to the end of the array and return the length of the new array

Example:

var arr = [11,33,55,77,66,44,22];
document.write(arr.push(88,99)+"<br>");//Returns the length of the array
document.write(arr);    //Returns a new array

Return:

9
11,33,55,77,66,44,22,88,99

arr.pop()

Delete and return the last element of the array

Example:

var arr = [11,33,55,77,66,44,22];
document.write(arr.pop()+"<br>");//Delete and return the last element of array arr
document.write(arr);    //Print arrays

Return:

22
11,33,55,77,66,44

arr.unshift(value1,value2,value3)

Add one or more elements to the beginning of an array and return the length of the new array

Example:

var arr = [11,33,55,77,66,44,22];
document.write(arr.unshift("aa","bb","cc")+"<br>");
document.write(arr);

Return:

10
aa,bb,cc,11,33,55,77,66,44,22

arr.shift()

Delete and return the first element of the array

Example:

var arr = [11,33,55,77,66,44,22];
document.write(arr.shift()+"<br>");
document.write(arr);

Return:

11
33,55,77,66,44,22

Topics: Javascript Python