public static String makeValidNetcdfObjectName(String name) {
    StringBuilder sb = new StringBuilder(name);

    while (sb.length() > 0) {
      int cp = sb.codePointAt(0);

      // First char must be [a-z][A-Z][0-9]_ | UTF8
      if (cp <= 0x7f) {
        if (!('A' <= cp && cp <= 'Z')
                && !('a' <= cp && cp <= 'z')
                && !('0' <= cp && cp <= '9')
                && cp != '_') {
          sb.deleteCharAt(0);
          continue;
        }
      }
      break;
    }

    for (int pos = 1; pos < sb.length(); ++pos) {
      int cp = sb.codePointAt(pos);

      // handle simple 0x00-0x7F characters here
      if (cp <= 0x7F) {
        if (cp < ' ' || cp > 0x7E || cp == '/') {  // control char, DEL, or forward-slash
          sb.deleteCharAt(pos);
          --pos;
        }
      }
    }

    while (sb.length() > 0) {
      int cp = sb.codePointAt(sb.length() - 1);

      if (cp <= 0x7f && Character.isWhitespace(cp)) {
        sb.deleteCharAt(sb.length() - 1);
      } else {
        break;
      }
    }

    if (sb.length() == 0) {
      throw new IllegalArgumentException(String.format("Illegal NetCDF object name: '%s'", name));
    }

    return sb.toString();
  }
public static String makeValidNetcdfObjectName ( String name ) { StringBuilder sb = new StringBuilder ( name ) ; while ( sb . length ( ) > 0 ) { int cp = sb . codePointAt ( 0 ) ; if ( cp <= 0x7f ) { if ( ! ( 'A' <= cp && cp <= 'Z' ) && ! ( 'a' <= cp && cp <= 'z' ) && ! ( '0' <= cp && cp <= '9' ) && cp != '_' ) { sb . deleteCharAt ( 0 ) ; continue ; } } break ; } for ( int pos = 1 ; pos < sb . length ( ) ; ++ pos ) { int cp = sb . codePointAt ( pos ) ; if ( cp <= 0x7F ) { if ( cp < ' ' || cp > 0x7E || cp == '/' ) { sb . deleteCharAt ( pos ) ; -- pos ; } } } while ( sb . length ( ) > 0 ) { int cp = sb . codePointAt ( sb . length ( ) - 1 ) ; if ( cp <= 0x7f && Character . isWhitespace ( cp ) ) { sb . deleteCharAt ( sb . length ( ) - 1 ) ; } else { break ; } } if ( sb . length ( ) == 0 ) { throw new IllegalArgumentException ( String . format ( "Illegal NetCDF object name: '%s'" , name ) ) ; } return sb . toString ( ) ; }
Convert a name to a legal netcdf-3 name.

@param name  the name to convert.
@return  the converted name.
@see #isValidNetcdfObjectName(String)