Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 org.cdlflex.ui.util;
 
 import java.util.Set;

String utility class.
 
 public final class Strings {

    
An empty String.
 
     public static final String EMPTY = "";
 
     private Strings() {
         // utility class
     }

    
Checks whether the given CharSequence is null or its length is zero.

Parameters:
sequence CharSequence to check
Returns:
true if the sequence is null or has length 0
 
     public static boolean isEmpty(CharSequence sequence) {
         return sequence == null || sequence.length() == 0;
     }

    
Takes a string of separated values, interprets them as a collection, retains unique values and joins them using the separator again. Example: A string "a b a", using a whitespace as separator, would yield "a b".

Parameters:
separator the separator
values the values to normalize together
Returns:
a normalized string
 
     public static String unique(String separatorString... values) {
         Set<Stringset = new HashSet<>(values.length);
 
         for (String value : values) {
             if (Strings.isEmpty(value)) {
                 continue;
             }
             set.addAll(Arrays.asList(value.trim().split(separator)));
         }
 
         return join(separatorset);
     }

    
Joins the string values of every given object with the given separator as glue.

Parameters:
separator the glue
values an array of objects
Returns:
a concatenated string
 
     public static String join(CharSequence separatorObject... values) {
         if (values == null || values.length == 0) {
             return ;
         }
 
         StringBuilder str = new StringBuilder();
         joinInto(strseparatorvalues);
         return str.toString();
     }

    
Joins the string values of every given object with the given separator as glue.

Parameters:
separator the glue
collection a collection of objects
<T> the collection type
Returns:
a concatenated string
 
     public static <T> String join(CharSequence separatorCollection<T> collection) {
         return join(separatorcollection.toArray(new Object[collection.size()]));
     }

    
Joins the string values of every given object with the given separator as glue.

Parameters:
separator the glue
collection a collection of objects
<T> the collection type
Returns:
a concatenated string
    public static <T> String join(CharSequence separatorIterable<T> collection) {
        if (collection == null) {
            return ;
        }
        StringBuilder str = new StringBuilder();
        joinInto(strseparatorcollection.iterator());
        return str.toString();
    }

    
Joins the string value sof every given object with the given separator as glue and appends those strings into the given StringBuilder.

Parameters:
str the string builder to append to
separator the glue
array an array of objects
    public static void joinInto(StringBuilder strCharSequence separatorObject[] array) {
        for (int i = 0; i < array.lengthi++) {
            str.append(valueOf(array[i]));
            if (i < (array.length - 1)) {
                str.append(separator);
            }
        }
    }

    
Joins the string value sof every given object with the given separator as glue and appends those strings into the given StringBuilder.

Parameters:
str the string builder to append to
separator the glue
<T> the collection type
collection a collection of objects
    public static <T> void joinInto(StringBuilder strCharSequence separatorIterable<T> collection) {
        joinInto(strseparatorcollection.iterator());
    }

    
Joins the string value sof every given object with the given separator as glue and appends those strings into the given StringBuilder.

Parameters:
str the string builder to append to
separator the glue
iterator a iterator of objects
    public static void joinInto(StringBuilder strCharSequence separatorIterator<?> iterator) {
        if (iterator.hasNext()) {
            str.append(valueOf(iterator.next()));
        }
        while (iterator.hasNext()) {
            str.append(separator);
            str.append(valueOf(iterator.next()));
        }
    }

    
Returns the string value of the given object.

Parameters:
object the object
Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
    public static String valueOf(Object object) {
        return String.valueOf(object);
    }
New to GrepCode? Check out our FAQ X