Home Web Front-end Front-end Q&A What are the methods of JavaScript string objects?

What are the methods of JavaScript string objects?

Dec 08, 2021 pm 01:52 PM
javascript string object Object methods

The methods of JavaScript string objects are: anchor(), big(), blink(), bold(), charAt(), concat(), fixed(), indexOf(), lastIndexOf(), replace(), search(), etc.

What are the methods of JavaScript string objects?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript String object is used to process strings, which provides a large number of methods for manipulating strings, as well as some properties.

The syntax format for creating a String object is as follows:

var val = new String(value);
var val = String(value);
Copy after login

The parameter value is the string or string object to be created.

In JavaScript, strings and string objects can be converted freely, so whether you are creating a string object or directly declaring a variable of string type, you can directly use the methods and properties provided in the string object. .

Methods of JavaScript string objects

The following table lists the methods and description information provided in the String object:

##big()Display the string in large fontblink()Display the flashing stringbold()Use bold to display the stringcharAt()Returns the character at the specified position charCodeAt()Returns the specified character Unicode encodingconcat()Concatenate stringsfixed() to typewriter Text display stringfontcolor()Use the specified color to display the stringfontsize()Use the specified size to display the stringfromCharCode()Convert the character encoding to a stringindexOf()Retrieve a string and get the position where the given string first appears in the string objectitalics()Use italics to display the stringlastIndexOf()Get the last occurrence position of the given string in the string objectlink()Display a string as a linklocaleCompare()Returns a number and uses that number to represent the character Whether the string object is greater than, less than, or equal to the given stringmatch()Match characters in the string according to the regular expressionreplace()Replace the substring matching the regular expressionsearch()Get the substring matching the regular expression The position where the matching string first appearsslice()Intercepts the fragment of the string and returns itsmall()Use small font size to display the stringsplit()Split the string into a string array based on the given characters strike()Use strikethrough to display stringsub() The string is displayed as a subscriptsubstr()Intercept the string of the specified length from the specified index positionsubstring ()Intercept the characters between two specified indexes in the stringsup()Display the string as a superscripttoLocaleLowerCase()Convert the string to lowercasetoLocaleUpperCase()Convert the string Convert to uppercasetoLowerCase()Convert the string to lowercasetoUpperCase()Convert the string to uppercasetoString()Return the stringvalueOf()Return the original value of a string object##The sample code is as follows:
MethodDescription
anchor()Creates an HTML anchor, that is, generates a <a> Label, the name attribute of the label is the parameter in the anchor() method
var str = new String(&#39;JavaScript教程&#39;);
document.write(str.anchor("myanchor") + "<br>");     // 生成一段 HTML 代码:<a name="myanchor">JavaScript教程</a>
document.write(str.big() + "<br>");                  // 生成一段 HTML 代码:<big>JavaScript教程</big>
document.write(str.blink() + "<br>");                // 生成一段 HTML 代码:<blink>JavaScript教程</blink>
document.write(str.bold() + "<br>");                 // 生成一段 HTML 代码:<b>JavaScript教程</b>
document.write(str.charAt(10) + "<br>");             // 获取 str 中的第 11 个字符,输出:教
document.write(str.charCodeAt(10) + "<br>");         // 获取 str 中第 11 个字符的 Unicode 编码,输出:25945
document.write(str.concat(" String 对象") + "<br>"); // 将字符串“ String 对象”拼接到字符串 str 之后,输出:JavaScript教程 String 对象
document.write(str.fixed() + "<br>");                // 生成一段 HTML 代码:<tt>JavaScript教程</tt>
document.write(str.fontcolor("red") + "<br>");       // 生成一段 HTML 代码:<font color="red">JavaScript教程</font>
document.write(str.fontsize(2) + "<br>");            // 生成一段 HTML 代码:<font size="2">JavaScript教程</font>
document.write(String.fromCharCode(72,69,76,76,79) + "<br>");             // 将 Unicode 编码转换为具体的字符,输出:HELLO
document.write(str.indexOf("Script") + "<br>");             // 获取字符串“Script”在 str 中首次出现的为,输出:4
document.write(str.italics() + "<br>");                     // 生成一段 HTML 代码:<i>JavaScript教程</i>
document.write(str.lastIndexOf("a") + "<br>");              // 获取字符串“a”在 str 中最后一次出现的位置,输出 3
document.write(str.link("http://c.biancheng.net/") + "<br>");  // 生成一段 HTML 代码:<a href="http://c.biancheng.net/">JavaScript教程</a>
document.write(str.localeCompare("JavaScript") + "<br>");       // 比较字符串对象与给定字符串,返回:1
document.write(str.match(/[abc]/g) + "<br>");                   // 根据正则 /[abc]/g 检索 str,返回:a,a,c
document.write(str.replace(/[abc]/g, "Y") + "<br>");            // 使用字符串“Y”替换正则 /[abc]/g 匹配的字符,返回:JYvYSYript教程
document.write(str.search(/[Script]/g) + "<br>");               // 获取与正则匹配的字符串首次出现的位置,返回:4
document.write(str.slice(6,11) + "<br>");           // 截取字符串(获取 str 中第 7 到第 11 个字符),返回:ript教
document.write(str.small() + "<br>");               // 生成一段 HTML 代码:<small>JavaScript教程</small>
document.write(str.split("a") + "<br>");            // 根据“a”将字符串 str 拆分为数组,返回:J,v,Script教程
document.write(str.strike() + "<br>");              // 生成一段 HTML 代码:<strike>JavaScript教程</strike>
document.write(str.sub() + "<br>");                 // 生成一段 HTML 代码:<sub>JavaScript教程</sub>
document.write(str.substr(3, 7) + "<br>");          // 从第 4 个字符开始,向后截取 7 个字符,返回:aScript
document.write(str.substring(3, 7) + "<br>");       // 截取字符串(获取 str 中第 4 到第 7 个字符),返回:aScr
document.write(str.sup() + "<br>");                 // 生成一段 HTML 代码:<sup>JavaScript教程</sup>
document.write(str.toLocaleLowerCase() + "<br>");   // 返回:javascript教程
document.write(str.toLocaleUpperCase() + "<br>");   // 返回:JAVASCRIPT教程
document.write(str.toLowerCase() + "<br>");         // 返回:javascript教程
document.write(str.toUpperCase() + "<br>");         // 返回:JAVASCRIPT教程
document.write(str.toString() + "<br>");            // 返回:JavaScript教程
document.write(str.valueOf() + "<br>");             // 返回:JavaScript教程
Copy after login

[Related recommendations:

javascript learning tutorial

The above is the detailed content of What are the methods of JavaScript string objects?. For more information, please follow other related articles on the PHP Chinese website!

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 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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

In-depth understanding of class methods and object methods in Go language In-depth understanding of class methods and object methods in Go language Apr 03, 2024 pm 09:27 PM

There are no traditional classes and objects in Go language, but structs and methods are used. Class methods are bound to the type and are used to operate on the entire type. Object methods are bound to object instances and are used to operate on a specific instance. The receivers of the two are different: the receiver of class methods is the type, while the receiver of object methods is the object instance pointer. There are also differences in naming conventions: class methods start with a capital letter, and object methods start with a lower case letter.

See all articles