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

net.jawr.web.util.StringUtils Maven / Gradle / Ivy

Go to download

Javascript/CSS bundling and compressing tool for java web apps. By using jawr resources are automatically bundled together and optionally minified and gzipped. Jawr provides tag libraries to reference a generated bundle either by id or by using the name of any of its members.

There is a newer version: 3.9
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses 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
 */
package net.jawr.web.util;

import java.util.regex.Pattern;

/**
 * 
 * Utility method for String manipulation.
 * The original code is available in commons-lang.
 * 
 * Below are the original authors of StringUtils.
 * 
 * @see java.lang.String
 * @author Apache Jakarta Turbine
 * @author Jon S. Stevens
 * @author Daniel L. Rall
 * @author Greg Coladonato
 * @author Ed Korthof
 * @author Rand McNeely
 * @author Stephen Colebourne
 * @author Fredrik Westermarck
 * @author Holger Krauth
 * @author Alexander Day Chaffee
 * @author Henning P. Schmiedehausen
 * @author Arun Mammen Thomas
 * @author Gary Gregory
 * @author Phil Steitz
 * @author Al Chou
 * @author Michael Davey
 * @author Reuben Sivan
 * @author Chris Hyzer
 * @author Scott Johnson
 * @since 1.0
 * @version $Id: StringUtils.java 635447 2008-03-10 06:27:09Z bayard $
 */
public class StringUtils {

	/** The new line separator */
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");
	
	/**
	 * \u000a linefeed LF ('\n').
	 * 
	 * @see JLF: Escape Sequences for Character and String
	 *      Literals
	 * @since 2.2
	 */
	public static final char LF = '\n';

	/**
	 * \u000d carriage return CR ('\r').
	 * 
	 * @see JLF: Escape Sequences for Character and String
	 *      Literals
	 * @since 2.2
	 */
	public static final char CR = '\r';

	/**
	 * The empty String "".
	 * 
	 * @since 2.0
	 */
	public static final String EMPTY = "";

	// Empty checks
	// -----------------------------------------------------------------------
	/**
	 * 

* Checks if a String is empty ("") or null. *

* *
	 * StringUtils.isEmpty(null)      = true
	 * StringUtils.isEmpty("")        = true
	 * StringUtils.isEmpty(" ")       = false
	 * StringUtils.isEmpty("bob")     = false
	 * StringUtils.isEmpty("  bob  ") = false
	 * 
* *

* NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank(). *

* * @param str the String to check, may be null * @return true if the String is empty or null */ public static boolean isEmpty(String str) { return str == null || str.length() == 0; } /** *

Checks if a String is not empty ("") and not null.

* *
     * StringUtils.isNotEmpty(null)      = false
     * StringUtils.isNotEmpty("")        = false
     * StringUtils.isNotEmpty(" ")       = true
     * StringUtils.isNotEmpty("bob")     = true
     * StringUtils.isNotEmpty("  bob  ") = true
     * 
* * @param str the String to check, may be null * @return true if the String is not empty and not null */ public static boolean isNotEmpty(String str) { return !StringUtils.isEmpty(str); } // Count matches // ----------------------------------------------------------------------- /** *

* Counts how many times the substring appears in the larger String. *

* *

* A null or empty ("") String input returns 0. *

* *
	 * StringUtils.countMatches(null, *)       = 0
	 * StringUtils.countMatches("", *)         = 0
	 * StringUtils.countMatches("abba", null)  = 0
	 * StringUtils.countMatches("abba", "")    = 0
	 * StringUtils.countMatches("abba", "a")   = 2
	 * StringUtils.countMatches("abba", "ab")  = 1
	 * StringUtils.countMatches("abba", "xxx") = 0
	 * 
* * @param str the String to check, may be null * @param sub the substring to count, may be null * @return the number of occurrences, 0 if either String is null */ public static int countMatches(String str, String sub) { if (isEmpty(str) || isEmpty(sub)) { return 0; } int count = 0; int idx = 0; while ((idx = str.indexOf(sub, idx)) != -1) { count++; idx += sub.length(); } return count; } // Chopping // ----------------------------------------------------------------------- /** *

* Remove the last character from a String. *

* *

* If the String ends in \r\n, then remove both of them. *

* *
	 * StringUtils.chop(null)          = null
	 * StringUtils.chop("")            = ""
	 * StringUtils.chop("abc \r")      = "abc "
	 * StringUtils.chop("abc\n")       = "abc"
	 * StringUtils.chop("abc\r\n")     = "abc"
	 * StringUtils.chop("abc")         = "ab"
	 * StringUtils.chop("abc\nabc")    = "abc\nab"
	 * StringUtils.chop("a")           = ""
	 * StringUtils.chop("\r")          = ""
	 * StringUtils.chop("\n")          = ""
	 * StringUtils.chop("\r\n")        = ""
	 * 
* * @param str the String to chop last character from, may be null * @return String without last character, null if null String input */ public static String chop(String str) { if (str == null) { return null; } int strLen = str.length(); if (strLen < 2) { return EMPTY; } int lastIdx = strLen - 1; String ret = str.substring(0, lastIdx); char last = str.charAt(lastIdx); if (last == LF) { if (ret.charAt(lastIdx - 1) == CR) { return ret.substring(0, lastIdx - 1); } } return ret; } /** *

Removes separator from the end of * str if it's there, otherwise leave it alone.

* *

NOTE: This method changed in version 2.0. * It now more closely matches Perl chomp. * For the previous behavior, use {@link #substringBeforeLast(String, String)}. * This method uses {@link String#endsWith(String)}.

* *
     * StringUtils.chomp(null, *)         = null
     * StringUtils.chomp("", *)           = ""
     * StringUtils.chomp("foobar", "bar") = "foo"
     * StringUtils.chomp("foobar", "baz") = "foobar"
     * StringUtils.chomp("foo", "foo")    = ""
     * StringUtils.chomp("foo ", "foo")   = "foo "
     * StringUtils.chomp(" foo", "foo")   = " "
     * StringUtils.chomp("foo", "foooo")  = "foo"
     * StringUtils.chomp("foo", "")       = "foo"
     * StringUtils.chomp("foo", null)     = "foo"
     * 
* * @param str the String to chomp from, may be null * @param separator separator String, may be null * @return String without trailing separator, null if null String input */ public static String chomp(String str, String separator) { if (isEmpty(str) || separator == null) { return str; } if (str.endsWith(separator)) { return str.substring(0, str.length() - separator.length()); } return str; } /** *

Remove a value if and only if the String ends with that value.

* * @param str the String to chomp from, must not be null * @param sep the String to chomp, must not be null * @return String without chomped ending * @throws NullPointerException if str or sep is null */ public static String chompLast(String str, String sep) { if (str.length() == 0) { return str; } String sub = str.substring(str.length() - sep.length()); if (sep.equals(sub)) { return str.substring(0, str.length() - sep.length()); } return str; } /** * Split the String passed in parameter, the trailing empty string are kept. * @param str the string to split * @param separator the separator * @return the splitted string */ public static String[] split(String str, String separator){ return Pattern.compile(separator).split(str, -1); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy