Day18. Number of Segments in a String(434)

问题描述:
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.

Example

Input: "Hello, my name is John"
Output: 5
/**
 * @param {string} s
 * @return {number}
 */
var countSegments = function(s) {
    var count = 0; 
    var arr = s.split(' ');
    for(var i in arr){
        if(arr[i] !== ""){
            count++;
        }
    }
     return count;
};
    原文作者:前端伊始
    原文地址: https://www.jianshu.com/p/47f969fe9bac
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞