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

后端 未结 17 1667
后悔当初
后悔当初 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条回答
  •  猫巷女王i
    2020-12-05 04:35

    How about this:

    var str = "", abbr = "";
    str = "Java Script Object Notation";
    str = str.split(' ');
    for (i = 0; i < str.length; i++) {
        abbr += str[i].substr(0,1);
    }
    alert(abbr);
    

    Working Example.

提交回复
热议问题