Java String Length Get lengthWithinLimits(String string, int min, int max)

Here you can find the source of lengthWithinLimits(String string, int min, int max)

Description

Checks if a String's length is greater than or equal to some 'min' value, and less than or equal to some 'max' value.

License

Apache License

Parameter

Parameter Description
string The String value whose length is being checked.
min The minimum allowed value for the String.
max The maximum allowed value for the String.

Return

Returns false if the String is null, 'min' is greater than 'max', or if the String's length is not within the bounds; otherwise, true is returned.

Declaration

public static boolean lengthWithinLimits(String string, int min, int max) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 The Regents of the University of California
 * /* w  w  w . j  av  a2  s.co m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

public class Main {
    /**
     * Checks if a String's length is greater than or equal to some 'min' 
     * value, and less than or equal to some 'max' value. The String should not
     * be null, but if it is, false is returned.
     * 
     * @param string The String value whose length is being checked.
     * 
     * @param min The minimum allowed value for the String.
     * 
     * @param max The maximum allowed value for the String.
     * 
     * @return Returns false if the String is null, 'min' is greater than 
     *          'max', or if the String's length is not within the bounds; 
     *          otherwise, true is returned.
     */
    public static boolean lengthWithinLimits(String string, int min, int max) {
        if (string == null) {
            return false;
        }

        if (min > max) {
            return false;
        }

        int stringLength = string.length();
        return ((stringLength >= min) && (stringLength <= max));
    }
}

Related

  1. lengthOfString(final String cadena)
  2. lengthTokensField(String fieldName)
  3. lengthToString(double value, boolean proportional)
  4. lengthUCP(String s)
  5. lengthUnicode(String s)