ES6 |细绳
ES6 JavaScript 中有两种字符串类型,字符串原语和字符串对象。它通过辅助方法包装了 JavaScript 的字符串原语。有一些辅助方法。
- 字符串原语:当调用包装器类型的方法时,原语会自动装箱到其包装器类型。它没有辅助方法,它只不过是一个指向原始数据内存引用的指针。所以在这里它包装到一个字符串包装器。 (var i = 1)在这里它包装到一个整数包装器。
- 字符串对象:这是一个字符串对象。 String 对象是在 String 类的帮助下创建的。它使用许多辅助方法包装字符串原始数据类型。
语法:在 ES6 JavaScript 中有以下三个属性:
var stringobject = new String(string);
字符串属性:Properties Description Constructor This property returns a reference to the string function which created the instance prototype. Length This property returns the length of the string. Prototype This property a global property which is available with almost all the objects. This allows you to add properties and methods to an object, where the prototyped object has different behavior compared to string primitive auto-boxing.
下面的示例说明了这些属性:
例子:
HTML
HTML
输出:
Constructor Property = function hero(id, name) { this.id = id; this.name = name; }
Length of emp.name(GeeksforGeeks) = 13
Prototype(emp1.name=Courses) group = Edutech
Prototype(emp.name=GeeksforGeeks) group = Edutech
字符串对象的方法:这些是字符串对象中可用的方法列表。Methods Description charAt(character) This methods returns the character at the specified index. charCodet(character) This methods returns the ascii code of the character at the specified index. concat(string) This methods returns the concatenated string. indexOf(string) This methods returns the starting index of the given string. lastIndexOf(string) This methods returns the starting index of the given string’s last occurrence. localeCompare(string) This methods returns 0, if the compared strings the equal, return 1 or -1 if the compared strings are not equal and 1 and -1 depending on the sorted order. match(RegExp, string) returns the match of the RegExp. replace(RegExp, string) returns the replaced RegExp in the string. search(RegExp) This methods searches for the given word or regex. If a string is passed as a parameter it gets converted into RegExp by using new RegExp(obj). slice(startIndex, endIndex) This methods returns the string from the startIndex to endIndex(excluding) at the specified index. substr(startIndex, length) returns the string from the startIndex to startIndex+length(excluding) at the specified index. substring(startIndex, endIndex) returns the string from the startIndex to endIndex(excluding) at the specified index. split(“separator”, number of splits) returns an array which got splited with the separator and given b=number of splits, if splits are not mentioned then return the whole array. toLocalLowerCase() returns a string in lowercase with the current locale. toLocalUpperCase() returns a string in uppercase with the current locale. toLowerCase() returns the calling string value converted to lowercase. toupperCase() returns the character at the specified index. toString() returns the calling string value converted to uppercase. valueOf() returns the primitive value of a String object.
下面的示例将说明这些方法:
例子:
HTML
输出:
str.charAt(0) is = A
str.charCodeAt(0) is = 65
str1.concat('=>Geeks?') = Computer Science Portal for Geeks=>Geeks?
str.indexOf('of') = -1
str.lastIndexOf('of') = -1
str.localeCompare( 'A Online Computer Science Portal') = 0
str.localeCompare( 'Computer Science Portal for Geeks') = -1
str.localeCompare( 'A Computer Science Portal') = 1
Geeks doesn't exist in str.
str.slice(5, -1) = online Computer Science Portal
str.substr(2, 9) = Online Co
str.substring(2, 9) = Online
str.split(' ') = ["A","Online","Computer","Science","Portal"]
str.split(':', 4) = ["A Online Computer Science Portal"]
str.split('of', 4) = ["A Online Computer Science Portal"]
str1.toLocaleLowerCase( ) = computer science portal for geeks
str1.toLocaleUpperCase() = COMPUTER SCIENCE PORTAL FOR GEEKS
str1.toLowerCase( ) = computer science portal for geeks
str1.toUpperCase() = COMPUTER SCIENCE PORTAL FOR GEEKS
str1.toString() = Computer Science Portal for Geeks
str1.valueOf( ) = Computer Science Portal for Geeks