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 /** 032 * Returns a double quoted string. 033 * 034 * @param str 035 * a String 036 * @return {@code "str"} 037 */ 038 public static String dquote(final String str) { 039 return Chars.DQUOTE + str + Chars.DQUOTE; 040 } 041 042 /** 043 * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using 044 * {@link String#trim()} is empty. 045 * 046 * @param s 047 * the String to check, may be {@code null} 048 * @return {@code true} if the String is {@code null}, empty, or trims to empty. 049 */ 050 public static boolean isBlank(final String s) { 051 return s == null || s.trim().isEmpty(); 052 } 053 054 /** 055 * <p> 056 * Checks if a CharSequence is empty ("") or null. 057 * </p> 058 * 059 * <pre> 060 * Strings.isEmpty(null) = true 061 * Strings.isEmpty("") = true 062 * Strings.isEmpty(" ") = false 063 * Strings.isEmpty("bob") = false 064 * Strings.isEmpty(" bob ") = false 065 * </pre> 066 * 067 * <p> 068 * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is 069 * available in isBlank(). 070 * </p> 071 * 072 * <p> 073 * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence) 074 * </p> 075 * 076 * @param cs 077 * the CharSequence to check, may be null 078 * @return {@code true} if the CharSequence is empty or null 079 */ 080 public static boolean isEmpty(final CharSequence cs) { 081 return cs == null || cs.length() == 0; 082 } 083 084 /** 085 * Checks if a String is not blank. The opposite of {@link #isBlank(String)}. 086 * 087 * @param s 088 * 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 113 * the CharSequence to check, may be null 114 * @return {@code true} if the CharSequence is not empty and not null 115 */ 116 public static boolean isNotEmpty(final CharSequence cs) { 117 return !isEmpty(cs); 118 } 119 120 /** 121 * Returns a quoted string. 122 * 123 * @param str 124 * a String 125 * @return {@code 'str'} 126 */ 127 public static String quote(final String str) { 128 return Chars.QUOTE + str + Chars.QUOTE; 129 } 130 131 /** 132 * <p> 133 * Removes control characters (char <= 32) from both ends of this String returning {@code null} if the String is 134 * empty ("") after the trim or if it is {@code null}. 135 * 136 * <p> 137 * The String is trimmed using {@link String#trim()}. Trim removes start and end characters <= 32. 138 * </p> 139 * 140 * <pre> 141 * Strings.trimToNull(null) = null 142 * Strings.trimToNull("") = null 143 * Strings.trimToNull(" ") = null 144 * Strings.trimToNull("abc") = "abc" 145 * Strings.trimToNull(" abc ") = "abc" 146 * </pre> 147 * 148 * <p> 149 * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String) 150 * </p> 151 * 152 * @param str 153 * the String to be trimmed, may be null 154 * @return the trimmed String, {@code null} if only chars <= 32, empty or null String input 155 */ 156 public static String trimToNull(final String str) { 157 final String ts = str == null ? null : str.trim(); 158 return isEmpty(ts) ? null : ts; 159 } 160 161 private Strings() { 162 // empty 163 } 164}