code: public static int getTrimmedLength(CharSequence s) {
          int len = s.length();
  
          int start = 0;
          while (start < len && s.charAt(start) <= ' ') {
              start++;
          }
  
          int end = len;
          while (end > start && s.charAt(end - 1) <= ' ') {
              end--;
          }
  
          return end - start;
      }
docs: Returns the length that the specified CharSequence would have if
  spaces and control characters were trimmed from the start and end,
  as by {@link String#trim}.