001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.util; 018 019/** 020 * <em>Consider this class private.</em> 021 * 022 * @see <a href="http://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a> 023 */ 024public final class Strings { 025 026 /** 027 * The empty string. 028 */ 029 public static final String EMPTY = ""; 030 031 private Strings() { 032 // empty 033 } 034 035 /** 036 * Returns a double quoted string. 037 * 038 * @param str a String 039 * @return {@code "str"} 040 */ 041 public static String dquote(final String str) { 042 return Chars.DQUOTE + str + Chars.DQUOTE; 043 } 044 045 /** 046 * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using 047 * {@link String#trim()} is empty. 048 * 049 * @param s the String to check, may be {@code null} 050 * @return {@code true} if the String is {@code null}, empty, or trims to empty. 051 */ 052 public static boolean isBlank(final String s) { 053 return s == null || s.trim().isEmpty(); 054 } 055 056 /** 057 * <p> 058 * Checks if a CharSequence is empty ("") or null. 059 * </p> 060 * 061 * <pre> 062 * Strings.isEmpty(null) = true 063 * Strings.isEmpty("") = true 064 * Strings.isEmpty(" ") = false 065 * Strings.isEmpty("bob") = false 066 * Strings.isEmpty(" bob ") = false 067 * </pre> 068 * 069 * <p> 070 * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is 071 * available in isBlank(). 072 * </p> 073 * 074 * <p> 075 * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence) 076 * </p> 077 * 078 * @param cs the CharSequence to check, may be null 079 * @return {@code true} if the CharSequence is empty or null 080 */ 081 public static boolean isEmpty(final CharSequence cs) { 082 return cs == null || cs.length() == 0; 083 } 084 085 /** 086 * Checks if a String is not blank. The opposite of {@link #isBlank(String)}. 087 * 088 * @param s the String to check, may be {@code null} 089 * @return {@code true} if the String is non-{@code null} and has content after being trimmed. 090 */ 091 public static boolean isNotBlank(final String s) { 092 return !isBlank(s); 093 } 094 095 /** 096 * <p> 097 * Checks if a CharSequence is not empty ("") and not null. 098 * </p> 099 * 100 * <pre> 101 * Strings.isNotEmpty(null) = false 102 * Strings.isNotEmpty("") = false 103 * Strings.isNotEmpty(" ") = true 104 * Strings.isNotEmpty("bob") = true 105 * Strings.isNotEmpty(" bob ") = true 106 * </pre> 107 * 108 * <p> 109 * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence) 110 * </p> 111 * 112 * @param cs the CharSequence to check, may be null 113 * @return {@code true} if the CharSequence is not empty and not null 114 */ 115 public static boolean isNotEmpty(final CharSequence cs) { 116 return !isEmpty(cs); 117 } 118 119 /** 120 * Returns a quoted string. 121 * 122 * @param str a String 123 * @return {@code 'str'} 124 */ 125 public static String quote(final String str) { 126 return Chars.QUOTE + str + Chars.QUOTE; 127 } 128 129 /** 130 * <p> 131 * Removes control characters (char <= 32) from both ends of this String returning {@code null} if the String is 132 * empty ("") after the trim or if it is {@code null}. 133 * 134 * <p> 135 * The String is trimmed using {@link String#trim()}. Trim removes start and end characters <= 32. 136 * </p> 137 * 138 * <pre> 139 * Strings.trimToNull(null) = null 140 * Strings.trimToNull("") = null 141 * Strings.trimToNull(" ") = null 142 * Strings.trimToNull("abc") = "abc" 143 * Strings.trimToNull(" abc ") = "abc" 144 * </pre> 145 * 146 * <p> 147 * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String) 148 * </p> 149 * 150 * @param str the String to be trimmed, may be null 151 * @return the trimmed String, {@code null} if only chars <= 32, empty or null String input 152 */ 153 public static String trimToNull(final String str) { 154 final String ts = str == null ? null : str.trim(); 155 return isEmpty(ts) ? null : ts; 156 } 157}