Topic Requirements
The function convertToCamelCase
of perfecting functions. The function convertToCamelCase
converts the passed-in string argument string
to the hump format and returns the converted string. The specific requirements are as follows:
- The argument string is a string of words that are concatenated with an underscore (-) to capitalize the first letter of the second non-empty word, such as the result of a-webkit-border-radius conversion of Webkitborderradius.
- Returns the converted String
functionconverttocamelcase (str) {//remove an underscore delimiter to get an array of words varStrarr = Str.split ('-')); //If the first one is empty, remove the if(Strarr[0] = = = ") {strarr.shift (); } //traverse the second word to the last word and convert the first letter of the word to thank you for(vari = 1, len = strarr.length; i < Len; i++){ //if not empty, turn to uppercase if(Strarr[i]!== ") {Strarr[i]= Strarr[i][0].touppercase () + strarr[i].substring (1); } } returnStrarr.join (' ');}
Note: Check whether the first item is empty
Ideas:
- Remove the underscore connector from the parameter string, where you use the string split method split
- Gets an array of words that remove the underlined connector and saves it in a new variable
strArr
- You need to determine if the first item in the array is empty, or if it is empty. Such as
‘-a-b-c‘.split(‘-‘); // 返回的是 [‘‘, a, b, c] 第一项便为空字符串
- Use a looping statement to set the starting coordinates to
i = 1
be processed only from the second word
- The first letter of each word is called the string capitalization method toUpperCase
- The first letter of the change and the character after the word are stitched together again to become a new word string
- After looping, the array of words is stitched into a complete string, using an array of methods to join
The knowledge points used are as follows:
split()
method to split a string into an array of strings. As shown below:
"2:3:4:5".split(":") // 将返回 ["2", "3", "4", "5"] "|a|b|c".split("|") // 将返回 ["", "a", "b", "c"]
更多: http://www.w3school.com.cn/jsref/jsref_split.asp
- Intercept string substring
substring()
method is used to extract the character of a string that is mediated between two specified subscripts. As shown below:
var str = ‘Hello World!‘; console.log(str.substring(3)); // 将返回 lo world!
为w3school的定义&解释
MORE: http://www.w3school.com.cn/jsref/jsref_substring.asp
- String Conversion Capital toUpperCase ()/toLocaleUpperCase ()
toLocaleUpperCase()
method is used to convert a string to uppercase. As shown below:
var str = ‘Hello World!‘; console.log(str.toLocaleUpperCase()); // 将返回 HELLO WORLD!
更多:http://www.w3school.com.cn/jsref/jsref_toLocaleUpperCase.asp
http://www.w3school.com.cn/jsref/jsref_toUpperCase.asp
Definition and usage
The join () method is used to put all the elements in an array into a string.
The elements are delimited by the specified delimiter.
更多:
http://www.w3school.com.cn/jsref/jsref_join.asp
更多方法:http://www.w3school.com.cn/jsref/jsref_obj_string.asp
string conversion to hump format--js exercise