com.marringtons.string
Class Strings

java.lang.Object
  extended bycom.marringtons.string.Strings

public class Strings
extends Object

This is a class of String static helper methods providing String testing and manipulation methods.

Author:
Paul Marrington

Method Summary
static boolean isEmpty(String string)
          Return true if string is null or has no characters.
static boolean isNotEmpty(String string)
          Return true if string is not null or empty.
static String join(ArrayList parts, String with)
          Join an ArrayList of strings into a single string, separating elements with the specified string.
static String join(String[] parts)
          Join an array of strings into a single string, separating elements with comma space.
static String join(String[] parts, String with)
          Join an array of strings into a single string, separating elements with the specified string.
static String join(String[] parts, String with, int from)
          Join an array of strings into a single string, separating elements with the specified string.
static String join(String[] parts, String with, int from, int upTo)
          Join an array of strings into a single string, separating elements with the specified string.
static String remove(String from, int start, int charactersToRemove)
          Remove a portion of a string given the starting point and the number of character.
static String remove(String from, String toRemove)
          Remove the first occurrence of one string in another returning the portions both before and after.
static void sortDescendingByLength(String[] strings)
          Sort an array of strings by length - longest first.
static String[] split(String source)
          Split a comma separator string into an array of strings.
static String[] splitJava(String qualifiedName)
          Split a Java qualified name (package.package.Class.field) into an array of strings.
static String[] toArray(Collection collection)
          It is common to collect strings in an ArrayList but need an array to return.
static String[] toArray(Matcher matcher)
          Create an array of strings from the return groups of a pattern match.
static String toString(Object object)
          Using toString on an object will fail if the object is null - but this static method won't.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isNotEmpty

public static boolean isNotEmpty(String string)
Return true if string is not null or empty.
 if (Strings.isNotEmpty( myAnswer)) { System.out.println( "answered");
 

Parameters:
string - to check for content
Returns:
true if string is not null and not zero length.

isEmpty

public static boolean isEmpty(String string)
Return true if string is null or has no characters.
 if (Strings.isEmpty( myAnswer)) return;
 

Parameters:
string - to check for content
Returns:
true if string is null or has zero length.

split

public static String[] split(String source)
Split a comma separator string into an array of strings.
 String[] parts = Strings.split( "ab, cd, ef");
 parts[0].equals( "ab");
 parts[1].equals( "cd");
 parts[2].equals( "ef");
 

Parameters:
source - string to split
Returns:
array of strings created by splitting source string.

splitJava

public static String[] splitJava(String qualifiedName)
Split a Java qualified name (package.package.Class.field) into an array of strings.

Parameters:
qualifiedName - Java name to split.
Returns:
Entry 0 is the full string, and 1..n is the package path.

join

public static String join(String[] parts)
Join an array of strings into a single string, separating elements with comma space.
 String[] parts = { "ab", "cd", "ef" };
 Strings.join( parts).equals( "ab, cd, ef");
 

Parameters:
parts - array of elements
Returns:
the combined string

join

public static String join(String[] parts,
                          String with)
Join an array of strings into a single string, separating elements with the specified string.
 String[] parts = { "ab", "cd", "ef" };
 Strings.join( parts, " ").equals( "ab cd ef");
 

Parameters:
parts - array of elements
with - string to separate elements in the result
Returns:
the combined string

join

public static String join(String[] parts,
                          String with,
                          int from)
Join an array of strings into a single string, separating elements with the specified string.
 String[] parts = { "ab", "cd", "ef" };
 Strings.join( parts, " ", 1).equals( "cd ef");
 

Parameters:
parts - array of elements
with - string to separate elements in the result
from - starting point in array (1 to skip first element)
Returns:
the combined string

join

public static String join(String[] parts,
                          String with,
                          int from,
                          int upTo)
Join an array of strings into a single string, separating elements with the specified string.
 String[] parts = { "ab", "cd", "ef", "gh" };
 Strings.join( parts, " ", 1, parts.length - 1).equals( "cd ef");
 

Parameters:
parts - array of elements
with - string to separate elements in the result
from - starting point in array (1 to skip first element)
upTo - Index to join up to but not including
Returns:
the combined string

join

public static String join(ArrayList parts,
                          String with)
Join an ArrayList of strings into a single string, separating elements with the specified string.

Parameters:
parts - array of elements
with - string to separate elements in the result
Returns:
the combined string

toArray

public static String[] toArray(Collection collection)
It is common to collect strings in an ArrayList but need an array to return.

Parameters:
collection - of String to turn into an Array.
Returns:
Array of String

toArray

public static String[] toArray(Matcher matcher)
Create an array of strings from the return groups of a pattern match. Skips group 0 which is the whole match.

Parameters:
matcher - from a pattern match.
Returns:
Array of String - one for each group. It can have null entries.
See Also:
Matcher

sortDescendingByLength

public static void sortDescendingByLength(String[] strings)
Sort an array of strings by length - longest first.

Parameters:
strings -

toString

public static String toString(Object object)
Using toString on an object will fail if the object is null - but this static method won't.

Parameters:
object -
Returns:
String representation of object - or null if object is null.

remove

public static String remove(String from,
                            String toRemove)
Remove the first occurrence of one string in another returning the portions both before and after.

Parameters:
from - String to have a section removed from.
toRemove - String to remove from larger whole.
Returns:
New String with offending chunk removed.

remove

public static String remove(String from,
                            int start,
                            int charactersToRemove)
Remove a portion of a string given the starting point and the number of character.

Parameters:
from - String to have a section removed from.
start - Start position for the piece to cut out.
charactersToRemove - Characters to remove.
Returns:
New String with offending characters removed.


Copyright © 2005 Paul Marrington http://library.marringtons.com