limit the string Length - Java java.lang

Java examples for java.lang:String Truncate

Description

limit the string Length

Demo Code

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class Main{
    public static void main(String[] argv){
        String input = "java2s.com";
        int minLength = 2;
        int maxLength = 4;
        System.out.println(limitedLength(input,minLength,maxLength));
    }/* w w  w  . ja va2  s . c om*/
    
    public static boolean limitedLength(String input, int minLength,
            int maxLength) {
        if (isEmpty(input))
            return (minLength <= 0);
        return input.length() >= minLength && input.length() <= maxLength;
    }
    
    public static boolean isEmpty(String str) {
        return (str == null || str.trim().length() == 0);
    }
    
    public static String trim(String str) {
        return (str == null) ? null : str.trim();
    }
}

Related Tutorials