org.strbio.util
Class StringUtil

java.lang.Object
  extended by org.strbio.util.StringUtil

public class StringUtil
extends java.lang.Object

Useful functions for doing stuff with strings. There is a known bug in atod and atof.

sprintf Features:

sprintf Bugs:
  Version 1.41, 4/30/07 - moved tests to separate class,
    fixed bug in atod, atof
  Version 1.4, 6/12/02 - added split
  Version 1.3, 3/6/1 - added stripHTML
  Version 1.26, 10/5/99 - added containsDigit
  Version 1.25, 6/22/99 - added containsLetter
  Version 1.24, 3/29/99 - added convertLF flag, since we sometimes
    want to leave things alone.  Default is true, though.
  Version 1.23, 2/10/99 - made buffer size for printf 5120
  Version 1.22, 1/20/99 - fixed bug in tmpnam()
  Version 1.21, 6/18/98 - fixed bug with double/float and precision of 1.
  Version 1.2, 6/4/98 - added sprintf functions.
  Version 1.1, 4/27/98 - rewrote atof, atol, atod, atoi.  No longer
  uses corejava.
  Version 1.0, 4/7/98 - original version
  

Version:
1.41, 4/30/07
Author:
JMC

Field Summary
static char[] EOL
          The end of line character(s) on this system.
 
Constructor Summary
StringUtil()
           
 
Method Summary
static double atod(java.lang.String s)
           
static double atod(java.lang.String s, int offset)
           
static double atod(java.lang.String s, int offset, int end)
          Converts a string to a double, starting at 'offset', and continuing until just before character 'end'.
static float atof(java.lang.String s)
           
static float atof(java.lang.String s, int offset)
           
static float atof(java.lang.String s, int offset, int end)
          Converts a string to a float, starting at 'offset', and continuing until just before character 'end'.
static int atoi(java.lang.String s)
           
static int atoi(java.lang.String s, int offset)
           
static int atoi(java.lang.String s, int offset, int end)
          Converts a string to an int, starting at 'offset', and continuing until just before character 'end'.
static long atol(java.lang.String s)
           
static long atol(java.lang.String s, int offset)
           
static long atol(java.lang.String s, int offset, int end)
          Converts a string to a long, starting at 'offset', and continuing until just before character 'end'.
static boolean containsDigit(java.lang.String a)
          does the string contain a digit?
static boolean containsLetter(java.lang.String a)
          does the string contain a letter?
static java.lang.String replace(java.lang.String template, java.lang.String a, java.lang.String b)
          replace all occurences of String 'a' in a template with String 'b', returning a new String.
static java.lang.String replaceEOL(java.lang.String template)
          Converts a string to another string with \n replaced by the local EOL character.
static java.lang.String[] split(java.lang.String str)
          splits a string into an array of strings; wrapper for StringTokenizer.
static java.lang.String[] split(java.lang.String str, java.lang.String delim)
          splits a string into an array of strings; wrapper for StringTokenizer null delimiter is the same as the default for ST.
static int sprintf(char[] buffer, java.lang.String fmt)
          default convertLF is true.
static int sprintf(char[] buffer, java.lang.String fmt, boolean convertLF)
          Formats a string without any parameter substitution, but with formatting (i.e.
static int sprintf(char[] buffer, java.lang.String fmt, char x)
          default convertLF is true.
static int sprintf(char[] buffer, java.lang.String fmt, char x, boolean convertLF)
          Formats a char.
static int sprintf(char[] buffer, java.lang.String fmt, double x)
          default convertLF is true.
static int sprintf(char[] buffer, java.lang.String fmt, double x, boolean convertLF)
          formats a double.
static int sprintf(char[] buffer, java.lang.String fmt, float x)
          default convertLF is true.
static int sprintf(char[] buffer, java.lang.String fmt, float x, boolean convertLF)
          Formats a float.
static int sprintf(char[] buffer, java.lang.String fmt, int x)
          default convertLF is true.
static int sprintf(char[] buffer, java.lang.String fmt, int x, boolean convertLF)
          Formats an int.
static int sprintf(char[] buffer, java.lang.String fmt, long x)
          default convertLF is true.
static int sprintf(char[] buffer, java.lang.String fmt, long x, boolean convertLF)
          Formats a long.
static int sprintf(char[] buffer, java.lang.String fmt, java.lang.String x)
          default convertLF is true.
static int sprintf(char[] buffer, java.lang.String fmt, java.lang.String x, boolean convertLF)
          formats a String.
static java.lang.String stripHTML(java.lang.String a)
          Deprecated. use HTML.stripHTML
static java.lang.String tmpnam()
          Like tmpnam() in C, but stupider... doesn't put the file into a /tmp directory or whatever the local equivalent is.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOL

public static final char[] EOL
The end of line character(s) on this system.

Constructor Detail

StringUtil

public StringUtil()
Method Detail

split

public static final java.lang.String[] split(java.lang.String str,
                                             java.lang.String delim)
splits a string into an array of strings; wrapper for StringTokenizer null delimiter is the same as the default for ST. Returns null if there are no tokens in the input string, or other error condition.


split

public static final java.lang.String[] split(java.lang.String str)
splits a string into an array of strings; wrapper for StringTokenizer.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                boolean convertLF)
Formats a string without any parameter substitution, but with formatting (i.e. \n, %%). Returns number of characters printed, not including terminating null char.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt)
default convertLF is true.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                int x,
                                boolean convertLF)
Formats an int. This is about 10x faster than formatting a long.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                int x)
default convertLF is true.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                long x,
                                boolean convertLF)
Formats a long.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                long x)
default convertLF is true.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                char x,
                                boolean convertLF)
Formats a char.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                char x)
default convertLF is true.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                java.lang.String x,
                                boolean convertLF)
formats a String.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                java.lang.String x)
default convertLF is true.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                double x,
                                boolean convertLF)
formats a double.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                double x)
default convertLF is true.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                float x,
                                boolean convertLF)
Formats a float.


sprintf

public static final int sprintf(char[] buffer,
                                java.lang.String fmt,
                                float x)
default convertLF is true.


containsLetter

public static final boolean containsLetter(java.lang.String a)
does the string contain a letter?


containsDigit

public static final boolean containsDigit(java.lang.String a)
does the string contain a digit?


replace

public static final java.lang.String replace(java.lang.String template,
                                             java.lang.String a,
                                             java.lang.String b)
replace all occurences of String 'a' in a template with String 'b', returning a new String.

See Also:
String, String.replace(char, char)

stripHTML

public static final java.lang.String stripHTML(java.lang.String a)
Deprecated. use HTML.stripHTML

Strip all HTML in a string, returning a new String. HTML is considered anything between '<' and '>' characters. Bug: can't escape either open or close brackets. This now just calls HTML.stripHTML.


replaceEOL

public static final java.lang.String replaceEOL(java.lang.String template)
Converts a string to another string with \n replaced by the local EOL character. This will return the same string if local EOL is \n.


atof

public static final float atof(java.lang.String s,
                               int offset,
                               int end)
Converts a string to a float, starting at 'offset', and continuing until just before character 'end'. Beginning non-numbers are ignored, and the process is stopped when the result no longer looks like a number. The default end is the string length, and the default offset is 0. This function will throw an exception if you give it an illegal offset or length. BUG: This will not read in a float with more than 20 digits after the decimal point!


atof

public static final float atof(java.lang.String s,
                               int offset)

atof

public static final float atof(java.lang.String s)

atod

public static final double atod(java.lang.String s,
                                int offset,
                                int end)
Converts a string to a double, starting at 'offset', and continuing until just before character 'end'. Beginning non-numbers are ignored, and the process is stopped when the result no longer looks like a number. The default end is the string length, and the default offset is 0. This function will throw an exception if you give it an illegal offset or length. BUG: This will not read in a double with more than 20 digits after the decimal point.


atod

public static final double atod(java.lang.String s,
                                int offset)

atod

public static final double atod(java.lang.String s)

atol

public static final long atol(java.lang.String s,
                              int offset,
                              int end)
Converts a string to a long, starting at 'offset', and continuing until just before character 'end'. Beginning non-numbers are ignored, and the process is stopped when the result no longer looks like a number. The default end is the string length, and the default offset is 0. This function will throw an exception if you give it an illegal offset or length.


atol

public static final long atol(java.lang.String s,
                              int offset)

atol

public static final long atol(java.lang.String s)

atoi

public static final int atoi(java.lang.String s,
                             int offset,
                             int end)
Converts a string to an int, starting at 'offset', and continuing until just before character 'end'. Beginning non-numbers are ignored, and the process is stopped when the result no longer looks like a number. The default end is the string length, and the default offset is 0. This function will throw an exception if you give it an illegal offset or length.


atoi

public static final int atoi(java.lang.String s,
                             int offset)

atoi

public static final int atoi(java.lang.String s)

tmpnam

public static final java.lang.String tmpnam()
Like tmpnam() in C, but stupider... doesn't put the file into a /tmp directory or whatever the local equivalent is. Without the Thread.sleep call, could return the same value if called twice in a row.