Home > Article > Web Front-end > What are the methods of JavaScript string objects?
The methods of JavaScript string objects are: anchor(), big(), blink(), bold(), charAt(), concat(), fixed(), indexOf(), lastIndexOf(), replace(), search(), etc.
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:
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 |
Display the string in large font | |
Display the flashing string | |
Use bold to display the string | |
Returns the character at the specified position | |
Returns the specified character Unicode encoding | |
Concatenate strings | |
to typewriter Text display string | |
Use the specified color to display the string | |
Use the specified size to display the string | |
Convert the character encoding to a string | |
Retrieve a string and get the position where the given string first appears in the string object | |
Use italics to display the string | |
Get the last occurrence position of the given string in the string object | |
Display a string as a link | |
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 string | |
Match characters in the string according to the regular expression | |
Replace the substring matching the regular expression | |
Get the substring matching the regular expression The position where the matching string first appears | |
Intercepts the fragment of the string and returns it | |
Use small font size to display the string | |
Split the string into a string array based on the given characters | |
Use strikethrough to display string | |
The string is displayed as a subscript | |
Intercept the string of the specified length from the specified index position | |
Intercept the characters between two specified indexes in the string | |
Display the string as a superscript | |
Convert the string to lowercase | |
Convert the string Convert to uppercase | |
Convert the string to lowercase | |
Convert the string to uppercase | |
Return the string | |
Return the original value of a string object |
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 tutorialThe 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!