Home  >  Article  >  Web Front-end  >  What are the methods of JavaScript string objects?

What are the methods of JavaScript string objects?

青灯夜游
青灯夜游Original
2021-12-08 13:52:232834browse

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

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:
Method Description
anchor() Creates an HTML anchor, that is, generates a 3499910bf9dac5ae3c52d5ede7383485 Label, the name attribute of the label is the parameter in the anchor() method
var str = new String('JavaScript教程');
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教程

[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:
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