View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.util;
18  
19  /**
20   * <em>Consider this class private.</em>
21   * 
22   * @see <a href="http://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>
23   */
24  public final class Strings {
25  
26      /**
27       * The empty string.
28       */
29      public static final String EMPTY = "";
30  
31      private Strings() {
32          // empty
33      }
34  
35      /**
36       * Returns a double quoted string.
37       * 
38       * @param str a String
39       * @return {@code "str"}
40       */
41      public static String dquote(final String str) {
42          return Chars.DQUOTE + str + Chars.DQUOTE;
43      }
44  
45      /**
46       * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using
47       * {@link String#trim()} is empty.
48       *
49       * @param s the String to check, may be {@code null}
50       * @return {@code true} if the String is {@code null}, empty, or trims to empty.
51       */
52      public static boolean isBlank(final String s) {
53          return s == null || s.trim().isEmpty();
54      }
55  
56      /**
57       * <p>
58       * Checks if a CharSequence is empty ("") or null.
59       * </p>
60       *
61       * <pre>
62       * Strings.isEmpty(null)      = true
63       * Strings.isEmpty("")        = true
64       * Strings.isEmpty(" ")       = false
65       * Strings.isEmpty("bob")     = false
66       * Strings.isEmpty("  bob  ") = false
67       * </pre>
68       *
69       * <p>
70       * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is
71       * available in isBlank().
72       * </p>
73       *
74       * <p>
75       * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)
76       * </p>
77       *
78       * @param cs the CharSequence to check, may be null
79       * @return {@code true} if the CharSequence is empty or null
80       */
81      public static boolean isEmpty(final CharSequence cs) {
82          return cs == null || cs.length() == 0;
83      }
84  
85      /**
86       * Checks if a String is not blank. The opposite of {@link #isBlank(String)}.
87       *
88       * @param s the String to check, may be {@code null}
89       * @return {@code true} if the String is non-{@code null} and has content after being trimmed.
90       */
91      public static boolean isNotBlank(final String s) {
92          return !isBlank(s);
93      }
94  
95      /**
96       * <p>
97       * Checks if a CharSequence is not empty ("") and not null.
98       * </p>
99       *
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 &lt;= 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 &lt;= 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 &lt;= 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 }