number-of-segments-in-a-string

https://leetcode.com/problems/number-of-segments-in-a-string/

package com.company;


class Solution {
    public int countSegments(String s) {
        String[] strs = s.split(" ");
        int count = 0;
        for (String str : strs) {
            if (str.length() > 0) {
                count++;
            }
        }
        return count;
    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        String s = "Hello, my  name is John";

        Solution solution = new Solution();
        int ret  = solution.countSegments(s);

        // Your Codec object will be instantiated and called as such:
        System.out.printf("ret:%d
", ret);

        System.out.println();

    }

}
原文地址:https://www.cnblogs.com/charlesblc/p/6156090.html