jmri.util
Class StringUtil

java.lang.Object
  extended by jmri.util.StringUtil

public class StringUtil
extends Object

Common utility methods for working with Strings.

We needed a place to refactor common string-processing idioms in JMRI code, so this class was created. It's more of a library of procedures than a real class, as (so far) all of the operations have needed no state information.

In some cases, these routines use a Java 1.3 or later method, falling back to an explicit implementation when running on Java 1.1

Author:
Bob Jacobsen Copyright 2003

Constructor Summary
StringUtil()
           
 
Method Summary
static String appendTwoHexFromInt(int val, String inString)
          Quickly append an int to a String as exactly two hexadecimal characters
static byte[] bytesFromHexString(String s)
          Create a byte[] from a String containing hexadecimal values.
static String getNameFromState(int state, int[] states, String[] names)
          Starting with two arrays, one of names and one of corresponding numeric state values, find the name string that matches a given state value.
static String[] getNamesFromStateMasked(int state, int[] states, int[] masks, String[] names)
          Starting with three arrays, one of names, one of corresponding numeric state values, and one of masks for the state values, find the name string(s) that match a given state value
static int getStateFromName(String name, int[] states, String[] names)
          Starting with two arrays, one of names and one of corresponding numeric state values, find the state value that matches a given name string
static String hexStringFromBytes(byte[] bytes)
          Create a String containing hexadecimal values from a byte[].
static String join(Collection<String> s, String delimiter)
          Join a collection of strings, separated by a delimiter
static String join(String[] s, String delimiter)
          Join an array of strings, separated by a delimiter
static void numberSort(String[] values)
          Sort String[] representing numbers, in ascending order.
static String parenQuote(String in)
          If there's an unmatched ), quote it with \, and quote \ with \ too.
static void sort(Object[] values)
          This is a lexagraphic sort; lower case goes to the end.
static void sort(String[] values)
          This is a lexagraphic sort; lower case goes to the end.
static void sortUpperCase(Object[] values)
          This is a case-independent lexagraphic sort.
static String[] split(String input, String divider)
          Split a string into an array of Strings, at a particular divider.
static List<String> splitParens(String in)
           
static String to8Bits(int val, boolean msbLeft)
          Convert a small number to eight 1/0 characters.
static String twoHexFromInt(int val)
          Convert an int to a exactly two hexadecimal characters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringUtil

public StringUtil()
Method Detail

getStateFromName

public static int getStateFromName(String name,
                                   int[] states,
                                   String[] names)
Starting with two arrays, one of names and one of corresponding numeric state values, find the state value that matches a given name string

Returns:
-1 if not found

getNamesFromStateMasked

public static String[] getNamesFromStateMasked(int state,
                                               int[] states,
                                               int[] masks,
                                               String[] names)
Starting with three arrays, one of names, one of corresponding numeric state values, and one of masks for the state values, find the name string(s) that match a given state value

Returns:
empty array if none found

getNameFromState

public static String getNameFromState(int state,
                                      int[] states,
                                      String[] names)
Starting with two arrays, one of names and one of corresponding numeric state values, find the name string that matches a given state value. Only one may be returned.

Returns:
null if not found

twoHexFromInt

public static String twoHexFromInt(int val)
Convert an int to a exactly two hexadecimal characters

Parameters:
val -
Returns:
String exactly two characters long

appendTwoHexFromInt

public static String appendTwoHexFromInt(int val,
                                         String inString)
Quickly append an int to a String as exactly two hexadecimal characters

Parameters:
val - Value to append in hex
inString - String to be extended
Returns:
String exactly two characters long

to8Bits

public static String to8Bits(int val,
                             boolean msbLeft)
Convert a small number to eight 1/0 characters.

Parameters:
msbLeft - the MSB is on the left of the display

hexStringFromBytes

public static String hexStringFromBytes(byte[] bytes)
Create a String containing hexadecimal values from a byte[].

Parameters:
bytes - byte array. Can be zero length, but must not be null.
Returns:
String of hex values, ala "01 02 0A B1 21 ".

bytesFromHexString

public static byte[] bytesFromHexString(String s)
Create a byte[] from a String containing hexadecimal values.

Parameters:
s - String of hex values, ala "01 02 0A B1 21".
Returns:
byte array, with one byte for each pair. Can be zero length, but will not be null.

sort

public static void sort(String[] values)
This is a lexagraphic sort; lower case goes to the end. Identical entries are retained, so the output length is the same as the input length.

Parameters:
values -

sort

public static void sort(Object[] values)
This is a lexagraphic sort; lower case goes to the end. Identical entries are retained, so the output length is the same as the input length.

Parameters:
values -

sortUpperCase

public static void sortUpperCase(Object[] values)
This is a case-independent lexagraphic sort. Identical entries are retained, so the output length is the same as the input length.

Parameters:
values -

numberSort

public static void numberSort(String[] values)
                       throws NumberFormatException
Sort String[] representing numbers, in ascending order.

Parameters:
values -
Throws:
NumberFormatException

join

public static String join(Collection<String> s,
                          String delimiter)
Join a collection of strings, separated by a delimiter

Parameters:
s - collection of strings
delimiter -
Returns:
e.g. join({"abc","def,"ghi"}, ".") ==> "abc.def.ghi"

join

public static String join(String[] s,
                          String delimiter)
Join an array of strings, separated by a delimiter

Parameters:
s - collection of strings
delimiter -
Returns:
e.g. join({"abc","def,"ghi"}, ".") ==> "abc.def.ghi"

split

public static String[] split(String input,
                             String divider)
Split a string into an array of Strings, at a particular divider. This is similar to the new String.split method, except that this does not provide regular expression handling; the divider string is just a string.

Parameters:
input - String to split
divider - Where to divide the input; this does not appear in output

parenQuote

public static String parenQuote(String in)
If there's an unmatched ), quote it with \, and quote \ with \ too.


splitParens

public static List<String> splitParens(String in)


Copyright © 1997 - 2011 JMRI Community.
JMRI, DecoderPro, PanelPro, SoundPro, DispatcherPro and associated logos are our trademarks.

Additional information on copyright, trademarks and licenses is linked here.
Site hosted by: Get JMRI Model Railroad Interface at SourceForge.net. Fast, secure and Free Open Source software downloads