Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.

For example,

Input: "Hello, my name is John"

Output: 5

    public int countSegments(String s) {
        String trimmed = s.trim();
        if (trimmed.length() == 0)
            return 0;
        else
            return trimmed.split("\\s+").length;
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-02-18
  • 2022-12-23
猜你喜欢
  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
  • 2021-10-06
  • 2021-06-23
相关资源
相似解决方案