Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2010 Google Inc.
   *
   * Licensed 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 com.google.common.base;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 
 import  javax.annotation.Nullable;

Static utility methods pertaining to String or CharSequence instances.

Author(s):
Kevin Bourrillion
Since:
3
 
 public final class Strings {
   private Strings() {}

  
Returns the given string if it is non-null; the empty string otherwise.

Parameters:
string the string to test and possibly return
Returns:
string itself if it is non-null; "" if it is null
 
   public static String nullToEmpty(@Nullable String string) {
     return (string == null) ? "" : string;
   }

  
Returns the given string if it is nonempty; null otherwise.

Parameters:
string the string to test and possibly return
Returns:
string itself if it is nonempty; null if it is empty or null
 
   public static @Nullable String emptyToNull(@Nullable String string) {
     return isNullOrEmpty(string) ? null : string;
   }

  
Returns true if the given string is null or is the empty string.

Consider normalizing your string references with nullToEmpty. If you do, you can use String.isEmpty() instead of this method, and you won't need special null-safe forms of methods like String.toUpperCase either. Or, if you'd like to normalize "in the other direction," converting empty strings to null, you can use emptyToNull.

Parameters:
string a string reference to check
Returns:
true if the string is null or is the empty string
 
   public static boolean isNullOrEmpty(@Nullable String string) {
     return string == null || string.length() == 0; // string.isEmpty() in Java 6
   }

  
Returns a string, of length at least minLength, consisting of string prepended with as many copies of padChar as are necessary to reach that length. For example,
  • padStart("7", 3, '0') returns "007"
  • padStart("2010", 3, '0') returns "2010"

See Formatter for a richer set of formatting capabilities.

Parameters:
string the string which should appear at the end of the result
minLength the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.
padChar the character to insert at the beginning of the result until the minimum length is reached
Returns:
the padded string
 
   public static String padStart(String stringint minLengthchar padChar) {
     checkNotNull(string);  // eager for GWT.
     if (string.length() >= minLength) {
       return string;
    }
    StringBuilder sb = new StringBuilder(minLength);
    for (int i = string.length(); i < minLengthi++) {
      sb.append(padChar);
    }
    sb.append(string);
    return sb.toString();
  }

  
Returns a string, of length at least minLength, consisting of string appended with as many copies of padChar as are necessary to reach that length. For example,
  • padEnd("4.", 5, '0') returns "4.000"
  • padEnd("2010", 3, '!') returns "2010"

See Formatter for a richer set of formatting capabilities.

Parameters:
string the string which should appear at the beginning of the result
minLength the minimum length the resulting string must have. Can be zero or negative, in which case the input string is always returned.
padChar the character to append to the end of the result until the minimum length is reached
Returns:
the padded string
  public static String padEnd(String stringint minLengthchar padChar) {
    checkNotNull(string);  // eager for GWT.
    if (string.length() >= minLength) {
      return string;
    }
    StringBuilder sb = new StringBuilder(minLength);
    sb.append(string);
    for (int i = string.length(); i < minLengthi++) {
      sb.append(padChar);
    }
    return sb.toString();
  }

  
Returns a string consisting of a specific number of concatenated copies of an input string. For example, repeat("hey", 3) returns the string "heyheyhey".

Parameters:
string any non-null string
count the number of times to repeat it; a nonnegative integer
Returns:
a string containing string repeated count times (the empty string if count is zero)
Throws:
IllegalArgumentException if count is negative
  public static String repeat(String stringint count) {
    checkNotNull(string);  // eager for GWT.
    checkArgument(count >= 0, "invalid count: %s"count);
    // If this multiplication overflows, a NegativeArraySizeException or
    // OutOfMemoryError is not far behind
    StringBuilder builder = new StringBuilder(string.length() * count);
    for (int i = 0; i < counti++) {
      builder.append(string);
    }
    return builder.toString();
  }
New to GrepCode? Check out our FAQ X