ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
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; }; ```