0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptで最後の単語の長さ取得

Posted at

#説明

  • 文字列がスペースで区切られたいくつかの単語で構成されている場合、文字列の最後の単語の長さを返します。

#例

入力: s = "Hello World"
出力: 5

#ソース

JavaScript
/**
 * @param {string}
 * @return {number}
 */
var lengthOfLastWord = function(s) {
    return s.trim().split(' ').pop().length;  
};

#説明

  • trim()で文字列の最初と最後のスペースをすべて削除します。
  • split(' ')文字列をスペースで分割して配列に変換。
  • pop()で最後の要素を返す。
  • .lengthで上記の要素の長さにアクセス。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?