当前位置:网站首页>JS string method Encyclopedia
JS string method Encyclopedia
2022-07-28 07:02:00 【Hahaha~】
One 、ES5 String method
( One ) Method of adding special effects
- length: attribute , Returns the length of the string
var str="abcde"
console.log(str.length) //5- anchor() establish HTML anchor
var str=" use Baidu Search "
re=str.anchor("http://www.baidu.com")
console.log(re) //<a name="http://www.baidu.com"> use Baidu Search </a>
- link() Display the string as a link
var str="Hello World"
re=str.link("http://www.baidu.com")
console.log(re) //<a href="http://www.baidu.com">Hello World</a>- big() Display strings in large font
var str="Hello World"
var re=str.big()
console.log(re) //<big>Hello World</big>- small() Use small font size to display strings
var str="Hello World"
var re=str.small()
console.log(re) //<small>Hello World</small>- blink() Show flashing string
var str="Hello World"
var re=str.blink()
console.log(re) //<blink>Hello World</blink>- bold() Use bold to display strings
var str="Hello World"
var re=str.bold()
console.log(re) //<bold>Hello World</bold>- sub() Display a string as a subscript
var str="Hello World"
var re=str.sub()
console.log(re) //<sub>Hello World</sub>- sup() Display the string as superscript
var str="Hello World"
var re=str.sup()
console.log(re) //<sup>Hello World</sup>- fontcolor() Use the specified color to display the string
var str="Hello World"
var re=str.fontcolor("red")
console.log(re) //<font color="red">Hello World</font>- fontsize() Use the specified size to display the string
var str="Hello World"
var re=str.fontsize("30px")
console.log(re) //<font size="30px">Hello World</font>- italics() Use italics to display strings
var str="Hello World"
var re=str.italics()
console.log(re) //<i>Hello World</i>( Two ) String conversion
- toLocaleLowerCase() Convert string to lowercase
var str="Hello World"
var str1=str.toLocaleLowerCase();
console.log(str); //Hello World
console.log(str1); //hello world- toLocaleUpperCase() Convert strings to uppercase
var str="Hello World"
var str1=str.toLocaleUpperCase();
console.log(str); //Hello World
console.log(str1); //HELLO WORLD- toLowerCase() Convert string to lowercase
var str="Hello World";
var str1=str.toLowerCase();
console.log(str); //Hello World
console.log(str1); //hello world
- toUpperCase() Convert strings to uppercase
var str="Hello World";
var str1=str.toUpperCase();
console.log(str); //Hello World
console.log(str1); //HELLO WORLD
- toString() Return string
var num=100;
var str=num.toString();
console.log(str) //"100"( 3、 ... and ) Common methods
- charAt() Returns the character at the specified subscript position if index be not in [0,str.length) Between, an empty string is returned
var str="hello world";
var str1=str.charAt(6);
console.log(str1); // w Subscript from 0 Start The space is also called
- charCodeAt() Returns the value of the character at the specified position Unicode code The return value is 0 - 65535 Integer between
var str="hello world";
var str1=str.charCodeAt(1);
var str2=str.charCodeAt(-1);
console.log(str1); //101
console.log(str2); //NaN
- concat() Connect the string and return a new string The original string is not changed
var str = "Hello";
var str1 = "World";
var str2 = str.concat("~",str1);// ==>"Hello" + "~" + "World";
console.log(str2) //"Hello~World"
- indexOf() Retrieving a string returns the subscript of the first occurrence of the specified substring in the string Return if not found -1
// Find the range [0,string.length - 1)
var str="Hello World";
var str1=str.indexOf("o");
var str2=str.indexOf("world");
var str3=str.indexOf("o",str1+1);
console.log(str1); //4 By default, only the first keyword position is found From the subscript 0 Start looking for
console.log(str2); //-1 Case sensitive No return found -1
console.log(str3); //7 The second parameter represents the subscript of the starting position
- lastIndexOf() Search the string from back to front and return the subscript of the last position of a specified substring in the string
var str="Hello World";
var str1=str.lastIndexOf("o");
var str2=str.lastIndexOf("world");
var str3=str.lastIndexOf("o",str1-1);
console.log(str1); //7
console.log(str2); //-1 Case sensitive No return found -1
console.log(str3); //4 The second parameter indicates which subscript to start from , If it is not written, it will start from the last character by default
- match() Match one or more regular expressions and return an array of all the searched keyword contents
var str="Can you can a can as a canner can a can?";
var reg=/can/ig;
var str1=str.match(reg);
console.log(str1); //['Can', 'can', 'can', 'can', 'can', 'can']
console.log(str.match("Hello")); //null- replace() Replace some characters with others Or replace a substring that matches a regular expression
var str="Hello World";
var reg=/o/ig; //o For the keyword to be replaced , Cannot quote , Otherwise, the replacement will not take effect ,i Ignore case ,g Represents a global lookup .
var str1=str.replace(reg,"**")
console.log(str1); //Hell** W**rld Case sensitive
- replaceAll() Replace some characters with others in the string Or replace all substrings that match the regular expression
var str = "Hello";
var str1 = str.replaceAll("l", "o");
console.log(str1); //Heooo
- search() Retrieve the value that matches the regular expression If the search is successful, the matching index value in the string will be returned. Otherwise, it will return -1
var str = "Hello World";
var str1 = str.search("hi");
console.log(str1) // -1
- trim() Remove spaces at both ends of the string
var str = " Hello World! ";
console.log(str.trim()); // Hello World!
( Two ) Intercept part of string
- split() Split a string into an array of strings Return a new array
var str="h e l l o";
var str1=str.split("");// Use an empty string as a delimiter , Every character of the string is divided
var str2=str.split(" "); // Use the space as the separator
var str3=str.split("",4); // The second parameter specifies the maximum length of the returned array
var string="1:2:3";
var str4=string.split(":");
console.log(str1); // ['h', ' ', 'e', ' ', 'l', ' ', 'l', ' ', 'o']
console.log(str2); // ['h', 'e', 'l', 'l', 'o']
console.log(str3); // ['h', ' ', 'e', ' ']
console.log(str4); // ['1', '2', '3']
- substr() Extract the characters of the specified length in the string from the starting index number
//substr(start, length)
var str="Hello World";
var str1=str.substr(1)
var str2=str.substr(1,3);
var str3=str.substr(-3,2);
console.log(str1); //ello World
console.log(str2); //ell
console.log(str3); //rl
- substring() Extract the character between two specified index marks in the string
//substring(start, end) Range [start, end)
var str="Hello World";
var str1=str.substring(2)
var str2=str.substring(2,2);
var str3=str.substring(2,7);
console.log(str1); //llo World
console.log(str2); // If two parameters are equal , Return length is 0 The empty string of
console.log(str3); //llo W
- slice() Extract a fragment of a string , And return the extracted part in the new string
//slice(start, end) Range [start, end) When start>end Returns an empty string
var str="Hello World";
var str1=str.slice(2); // If there is only one parameter , Then extract all strings from the beginning subscript to the end
var str2=str.slice(2,7); // Two parameters , The extraction subscript is 2, To the subscript 7 But it doesn't include the subscript 7 String
var str3=str.slice(-7,-2); // If it's a negative number ,-1 Is the last character of the string Extraction range is [-7,-2) String
console.log(str1); //llo World
console.log(str2); //llo W
console.log(str3); //o Wor
Two 、es6 New string method
( One ) Identification of substrings
includes(): Returns a Boolean value , Determine if the parameter string is found .
var str = "hello"
var re = str.includes("o")
console.log(re) //truestartsWith(): Returns a Boolean value , Judge whether the parameter string is in the head of the original string .
var str = "hello"
var re = str.endsWith("h")
console.log(re) //falseendsWith(): Returns a Boolean value , Judge whether the parameter string is at the end of the original string .
var str = "hello"
var re = str.startsWith("h")
console.log(re) //trueBe careful :
These three methods only return Boolean values , To know the position of the substring , Still have to use indexOf and lastIndexOf
If these three methods pass in regular expressions instead of strings , Will throw an error
IndexOf and lastIndexOf Will convert the regular expression to a string and search it
All three methods support the second parameter , Indicates where to start the search
( Two ) String filling ( completion )
padStart: Returns a new string , Represents a parameter string from the header ( left ) Complete the original string
var str = "100000"
var re = str.padStart(11, "6666")
console.log(re) //66666100000padEnd: Returns a new string , Indicates that a parameter string is used from the end ( On the right side ) Complete the original string
var str = "100000"
var re = str.padEnd(11, "6666")
console.log(re) //10000066666
- Two parameters : The first parameter is to specify the minimum length of the generated string , The second parameter is the string used to complete . If the second parameter is not specified , Fill with space by default
- If the specified length is less than or equal to the length of the original string , Return the original string
- If the length of the original string plus the completion string is greater than the specified length , The completion string that exceeds the number of digits is truncated
( 3、 ... and ) Duplicate string
- repeat(): Returns a new string , Indicates that the string is repeated a specified number of times and then returned
var str = "hello"
console.log(str.repeat(2)) //hellohello
- If the parameter is a decimal , Rounding down
- If the parameter is 0 to -1 Decimal between , You will get -0 , Equate to repeat 0 Time
- If the parameter is NaN, Equate to repeat 0 Time
- If the parameter is negative or Infinity , Will report a mistake
- If the parameter passed in is a string , The string is first converted to a number
( Four ) Template string
- The template string is equivalent to the enhanced version of the string , Use back quotes `
- Except as a normal string , It can also be used to define multiline strings , You can also add variables and expressions to a string
- The variable name is written in ${} in ,${} You can put JavaScript expression
// Wrap splice
var a = 123
var str = `hello${a}
h5
`
console.log(str) /* hello123
h5
*/
var r = parseInt(Math.random() * 225)
var g = parseInt(Math.random() * 225)
var b = parseInt(Math.random() * 225)
var rgb = 'rgb(' + r + ',' + g + ',' + b + ')'
var rgb2 = `rgb(${r},${g},${b})`
console.log(rgb) //rgb(156,61,50)
console.log(rgb2) //rgb(156,61,50)- Example
var str1="hello"
var r=200
var str2=`<div
style="color:rbg(${r},100,100)">${str1}<div>`
console.log(str2) /*<div
style="color:rbg(200,100,100)">hello<div>
*/边栏推荐
- Hdu-1097-a hard puzzle (fast power)
- Servlet
- Test life | second tier cities with an annual salary of more than 40W? How did you achieve 100% salary increase under the epidemic?
- Animation animation realizes the crossing (click) pause
- DHCP principle and configuration
- 1、 PXE overview and installation
- CentOS7部署MySQL数据库服务器
- Applets: lifecycle
- Clock tree analysis example
- PKU-2739-Sum of Consecutive Prime Numbers(筛素数法打表)
猜你喜欢

Technology sharing | sending requests using curl

QGraphicsView提升为QChartView

Technology sharing | detailed explanation of actual combat interface test request methods get, post

Applets: lifecycle

SSH service configuration

KVM hot migration

NFS shared storage service

Detailed explanation of LNMP construction process

Use powercli to create a custom esxi ISO image

Applet custom components - data, methods, and properties
随机推荐
Which brand of air conduction earphones is better? These four should not be missed
Qgraphicsview promoted to qchartview
Cocos2d-x learning notes Tile Map tiledmap
[learning notes] drive
JSON notes
Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones
Test interview questions collection (II) | test tools (with answers)
Applets: lifecycle
Ten thousand words summarize and realize the commonly used sorting and performance comparison
MySQL common commands
Ubuntu18.04 set up redis cluster [learning notes]
MySQL主从
Teach you three steps to complete the construction of the test monitoring system hand in hand
Installation and configuration of unit test framework jest with typescript
How to describe a bug and the definition and life cycle of bug level
shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)
MySQL主主
Esxi community nvme driver update v1.1
防火墙——iptables防火墙(四表五链、防火墙配置方法、匹配规则详解)
Which is the best air conduction Bluetooth headset? Air conduction Bluetooth headset ranking