All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jlibs.core.lang.StringUtil Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
/**
 * Copyright 2015 Santhosh Kumar Tekuri
 *
 * The JLibs authors license this file to you 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.
 */

package jlibs.core.lang;

import jlibs.core.graph.Convertor;
import jlibs.core.graph.Filter;
import jlibs.core.util.regex.TemplateMatcher;

import java.util.Collections;
import java.util.Iterator;
import java.util.StringTokenizer;

/**
 * @author Santhosh Kumar T
 */
public class StringUtil{
    @SuppressWarnings("StringEquality")
    public static boolean equalsIgnoreCase(String s1, String s2){
        return s1==s2 || (s1!=null && s2!=null && s1.equalsIgnoreCase(s2));
    }

    /**
     * if obj is null, returns empty string.
     * otherwise returns obj.toString()
     */
    public static String toString(Object obj){
        return obj==null ? "" : obj.toString();
    }

    /**
     * returns true if str is null or
     * its length is zero
     */
    public static boolean isEmpty(CharSequence str){
        return str==null || str.length()==0;
    }

    /**
     * returns true if str is null or
     * it contains only whitespaces.
     * 

* {@link Character#isWhitespace(char)} is used * to test for whitespace */ public static boolean isWhitespace(CharSequence str){ if(str!=null){ for(int i=0; i filter, String pattern, boolean tryEmptyVar){ if(!pattern.contains("${i}")) throw new IllegalArgumentException("pattern must have ${i}"); TemplateMatcher matcher = new TemplateMatcher("${", "}"); if(tryEmptyVar){ String value = matcher.replace(pattern, Collections.singletonMap("i", "")); if(filter.select(value)) return value; } int i = tryEmptyVar ? 2 : 1; while(true){ String value = matcher.replace(pattern, Collections.singletonMap("i", String.valueOf(i))); if(filter.select(value)) return value; i++; } } /** * Converts first character in str to uppercase. *

* This method can be called on string of any length. * * @param str string to be converted * @return string with first letter changed to uppercase */ public static String capitalize(String str){ if(str==null) return null; switch(str.length()){ case 0: return str; case 1: return str.toUpperCase(); default: return Character.toUpperCase(str.charAt(0))+str.substring(1); } } /** * Makes an underscored form from the expression in the string. *

* Examples: *

     * underscore("activeRecord")     // "active_record"
     * underscore("ActiveRecord")     // "active_record"
     * underscore("firstName")        // "first_name"
     * underscore("FirstName")        // "first_name"
     * underscore("name")             // "name"
     * 
* * @param camelCaseWord the camel-cased word that is to be converted; * @return a lower-cased version of the input, with separate words delimited by the underscore character. */ public static String underscore(String camelCaseWord){ if(camelCaseWord==null) return null; camelCaseWord = camelCaseWord.trim(); if(camelCaseWord.length()==0) return ""; camelCaseWord = camelCaseWord.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2"); camelCaseWord = camelCaseWord.replaceAll("([a-z\\d])([A-Z])", "$1_$2"); return camelCaseWord.toLowerCase(); } /** * Turns a non-negative number into an ordinal string used to * denote the position in an ordered sequence, such as 1st, 2nd, * 3rd, 4th * * @param number the non-negative number * @return the string with the number and ordinal suffix */ public static String ordinalize(int number){ int modulo = number%100; if(modulo>=11 && modulo<=13) return number+"th"; switch(number%10){ case 1: return number+"st"; case 2: return number+"nd"; case 3: return number+"rd"; default: return number+"th"; } } public static int[] toCodePoints(String str){ int count = str.codePointCount(0, str.length()); int[] codePoints = new int[count]; for(int cpIndex=0, charIndex=0; cpIndex String join(T[] array){ return join(array, ", ", null); } public static String join(T[] array, String separator){ return join(array, separator, null); } public static String join(T[] array, String separator, Convertor convertor){ StringBuilder buff = new StringBuilder(); boolean addSeparator = false; for(T item: array){ if(addSeparator) buff.append(separator); else addSeparator = true; if(item==null) buff.append("null"); else buff.append(convertor==null ? item.toString() : convertor.convert(item)); } return buff.toString(); } /*-------------------------------------------------[ Iterator Join ]---------------------------------------------------*/ public static String join(Iterator iter){ return join(iter, ", ", null); } public static String join(Iterator iter, String separator){ return join(iter, separator, null); } public static String join(Iterator iter, String separator, Convertor convertor){ StringBuilder buff = new StringBuilder(); boolean addSeparator = false; while(iter.hasNext()){ T item = iter.next(); if(addSeparator) buff.append(separator); else addSeparator = true; if(item==null) buff.append("null"); else buff.append(convertor==null ? item.toString() : convertor.convert(item)); } return buff.toString(); } /*-------------------------------------------------[ Literal ]---------------------------------------------------*/ public static String toLiteral(char ch, boolean useRaw){ if(ch=='\'') return "\\'"; else if(ch=='"') return "\""; else return StringUtil.toLiteral(String.valueOf(ch), useRaw); } public static String toLiteral(CharSequence str, boolean useRaw){ StringBuilder buf = new StringBuilder(str.length()+25); for(int i=0,len=str.length(); i=0x0020 && (useRaw || c<=0x007f)) // visible character in ascii buf.append(c); else{ buf.append("\\u"); String hex = Integer.toHexString(c); for(int j=4-hex.length(); j>0; j--) buf.append('0'); buf.append(hex); } } } return buf.toString(); } public static String fromLiteral(String str){ StringBuilder buf = new StringBuilder(); for(int i=0,len=str.length(); i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy