Length of Last Word
2016-06-16 07:51
621 查看
Given a string s consists of upper/lower-case alphabets and empty space characters
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s =
return
Subscribe to see which companies asked this question
Tags
String
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function (s) {
s = s.trim();
if (s.length === 0) {
return 0;
}
var arr = s.split(' ');
return arr[arr.length - 1].length;
};
' ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s =
"Hello World",
return
5.
Subscribe to see which companies asked this question
Tags
String
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function (s) {
s = s.trim();
if (s.length === 0) {
return 0;
}
var arr = s.split(' ');
return arr[arr.length - 1].length;
};
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android之获取手机上的图片和视频缩略图thumbnails
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- android上改变listView的选中颜色
- String.intern
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因