Get first letter of each word in a string, in JavaScript

后端 未结 17 1642
后悔当初
后悔当初 2020-12-05 04:00

How would you go around to collect the first letter of each word in a string, as in to receive an abbreviation?

Input: "Java Script Object

17条回答
  •  渐次进展
    2020-12-05 04:29

    This is made very simple with ES6

    string.split(' ').map(i => i.charAt(0))               //Inherit case of each letter
    string.split(' ').map(i => i.charAt(0)).toUpperCase() //Uppercase each letter
    string.split(' ').map(i => i.charAt(0)).toLowerCase() //lowercase each letter
    

    This ONLY works with spaces or whatever is defined in the .split(' ') method

    ie, .split(', ') .split('; '), etc..

提交回复
热议问题