Home Web Front-end JS Tutorial JavaScript study notes (3) String Introduction to string type_Basic knowledge

JavaScript study notes (3) String Introduction to string type_Basic knowledge

May 16, 2016 pm 05:52 PM
string type

1. Character methods charAt(), charCodeAt(), fromCharCode()

Copy code The code is as follows:

var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"
alert(stringValue[1]); //"e"
alert( stringValue.charCodeAt(1)); //101
alert(String.fromCharCode(104,101)); //"he"

2. Return substring methods slice(), substr (), substring()
The first parameter of the slice(), substring() method specifies the starting position of the substring, and the second parameter specifies the settlement position (excluding the end position), and the original string remains unchanged
The second parameter of substr() refers to the number of characters returned, the original string remains unchanged
Copy code Code As follows:

var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)) ; //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3,7)); //"lo w"
alert(stringValue.subtring(3,7)); //"lo w"
alert(stringValue.substr(3,7)); //"lo worl"

alert(stringValue.slice (-3)); //"rld", take the last 3 characters of the array
alert(stringValue.slice(-3)); //"rld", take the last 3 characters of the array

3. String position methods indexOf() and lastIndexOf()
The indexOf() method searches substrings from front to back. It can receive one parameter or two parameters. The first parameter specifies the substring to be searched. , the second parameter specifies to search backward from this position, if not found, it returns -1
lastIndexOf() method searches the substring from back to forward, and can receive one parameter or two parameters, the first parameter specifies the value to be searched Substring, the second parameter specifies to search forward from this position, if not found, return -1
Copy code The code is as follows:

var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7
alert(stringValue.indexOf("o",6)); //7
alert(stringValue.lastIndexOf("o",6)); //4

4. String case conversion methods toLowerCase() and toUpperCase()
toLowerCase() converts to lowercase, toUpperCase() converts to uppercase

5. String comparison localeCompare()
localeCompare() can compare English or Chinese, with uppercase letters in front and lowercase letters in the back

6. String sorting:
Copy code The code is as follows:

var stringValue= ["China","Nannan","Junjun"];
alert(stringValue.sort(stringCompare ));
//Ascending sorting function a-z
function stringCompare(value1,value2) {
return value1.localeCompare(value2); //Descending order z-a, value1 and value2 exchange positions
}
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

How to change network type to private or public in Windows 11 How to change network type to private or public in Windows 11 Aug 24, 2023 pm 12:37 PM

Setting up a wireless network is common, but choosing or changing the network type can be confusing, especially if you don't know the consequences. If you're looking for advice on how to change the network type from public to private or vice versa in Windows 11, read on for some helpful information. What are the different network profiles in Windows 11? Windows 11 comes with a number of network profiles, which are essentially sets of settings that can be used to configure various network connections. This is useful if you have multiple connections at home or office so you don't have to set it all up every time you connect to a new network. Private and public network profiles are two common types in Windows 11, but generally

Implementing dynamic arrays in Python: from beginner to proficient Implementing dynamic arrays in Python: from beginner to proficient Apr 21, 2023 pm 12:04 PM

Part 1 Let’s talk about the nature of Python sequence types. In this blog, let’s talk about Python’s various “sequence” classes and the three built-in commonly used data structures – list, tuple and character. The nature of the string class (str). I don’t know if you have noticed it, but these classes have an obvious commonality. They can be used to save multiple data elements. The most important function is: each class supports subscript (index) access to the elements of the sequence, such as using SyntaxSeq[i]​. In fact, each of the above classes is represented by a simple data structure such as an array. However, readers familiar with Python may know that these three data structures have some differences: for example, tuples and strings cannot be modified, while lists can.

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

How to use java's String class How to use java's String class Apr 19, 2023 pm 01:19 PM

1. Understanding String1. String in JDK First, let’s take a look at the source code of the String class in the JDK. It implements many interfaces. You can see that the String class is modified by final. This means that the String class cannot be inherited and there is no subclass of String. class, so that all people using JDK use the same String class. If String is allowed to be inherited, everyone can extend String. Everyone uses different versions of String, and two different people Using the same method shows different results, which makes it impossible to develop the code. Inheritance and method overriding not only bring flexibility, but also cause many subclasses to behave differently.

See all articles