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

后端 未结 17 1671
后悔当初
后悔当初 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:45

    If you came here looking for how to do this that supports non-BMP characters that use surrogate pairs:

    initials = str.split(' ')
                  .map(s => String.fromCodePoint(s.codePointAt(0) || '').toUpperCase())
                  .join('');
    

    Works in all modern browsers with no polyfills (not IE though)

提交回复
热议问题