Java String Sub String subStringWhiteSpaces(String str, int maxLen)

Here you can find the source of subStringWhiteSpaces(String str, int maxLen)

Description

Retorna uma sub string de tamanho maximo maxlen, dividindo a string original por espacos

License

Apache License

Parameter

Parameter Description
str a parameter
maxlen a parameter

Declaration

public static String subStringWhiteSpaces(String str, int maxLen) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w  w w . j  a v a  2s .c  o m
     * Retorna uma sub string de tamanho maximo maxlen, dividindo a string original por espacos
     * 
     * @param str
     * @param maxlen
     * @return
     */
    public static String subStringWhiteSpaces(String str, int maxLen) {
        if (str == null)
            return null;
        if (maxLen < 1)
            return "";

        int size = str.length();

        if (maxLen >= size)
            return str;

        String ini = str.substring(0, maxLen);
        String end = str.substring(maxLen, str.length());

        if (end.charAt(0) == ' ')
            return ini;

        int whitespace = ini.lastIndexOf(' ');
        if (whitespace == -1)
            return ini;

        return ini.substring(0, whitespace);

    }
}

Related

  1. substrings(String[] arr, int start, int end)
  2. subStringToInteger(String src, String start, String to)
  3. substringToLast(final String str, final String separator)
  4. substringUntil(String org, int begin, char term)
  5. substringUntilMatch(String string, String match)
  6. subStringWith3Point(String inputStr, int beginIndex, int length)
  7. subStrOccurences(String str, String findStr)
  8. subStrTime(String strTime)