uk.ac.starlink.ttools.func
Class Strings

java.lang.Object
  extended by uk.ac.starlink.ttools.func.Strings

public class Strings
extends Object

String manipulation and query functions.

Since:
2 Sep 2004
Author:
Mark Taylor (Starlink)

Method Summary
static String concat(String s1, String s2)
          Concatenates two strings.
static String concat(String s1, String s2, String s3)
          Concatenates three strings.
static String concat(String s1, String s2, String s3, String s4)
          Concatenates four strings.
static boolean contains(String whole, String sub)
          Determines whether a string contains a given substring.
static boolean endsWith(String whole, String end)
          Determines whether a string ends with a certain substring.
static boolean equals(String s1, String s2)
          Determines whether two strings are equal.
static boolean equalsIgnoreCase(String s1, String s2)
          Determines whether two strings are equal apart from possible upper/lower case distinctions.
static int length(String str)
          Returns the length of a string in characters.
static boolean matches(String str, String regex)
          Tests whether a string matches a given regular expression.
static String matchGroup(String str, String regex)
          Returns the first grouped expression matched in a string defined by a regular expression.
static String padWithZeros(long value, int ndigit)
          Takes an integer argument and returns a string representing the same numeric value but padded with leading zeros to a specified length.
static String replaceAll(String str, String regex, String replacement)
          Replaces all occurrences of a regular expression in a string with a different substring value.
static String replaceFirst(String str, String regex, String replacement)
          Replaces the first occurrence of a regular expression in a string with a different substring value.
static String[] split(String words)
          Splits a string into an array of space-separated words.
static String[] split(String words, String regex)
          Splits a string into an array of words separated by a given regular expression.
static boolean startsWith(String whole, String start)
          Determines whether a string starts with a certain substring.
static String substring(String str, int startIndex)
          Returns the last part of a given string.
static String substring(String str, int startIndex, int endIndex)
          Returns a substring of a given string.
static String toLowerCase(String str)
          Returns an uppercased version of a string.
static String toUpperCase(String str)
          Returns an uppercased version of a string.
static String trim(String str)
          Trims whitespace from both ends of a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

concat

public static String concat(String s1,
                            String s2)
Concatenates two strings. In most cases the same effect can be achieved by writing s1+s2, but blank values can sometimes appear as the string "null" if you do it like that.

Parameters:
s1 - first string
s2 - second string
Returns:
s1 followed by s2

concat

public static String concat(String s1,
                            String s2,
                            String s3)
Concatenates three strings. In most cases the same effect can be achieved by writing s1+s2+s3, but blank values can sometimes appear as the string "null" if you do it like that.

Parameters:
s1 - first string
s2 - second string
s3 - third string
Returns:
s1 followed by s2 followed by s3

concat

public static String concat(String s1,
                            String s2,
                            String s3,
                            String s4)
Concatenates four strings. In most cases the same effect can be achieved by writing s1+s2+s3+s4, but blank values can sometimes appear as the string "null" if you do it like that.

Parameters:
s1 - first string
s2 - second string
s3 - third string
s4 - fourth string
Returns:
s1 followed by s2 followed by s3 followed by s4

equals

public static boolean equals(String s1,
                             String s2)
Determines whether two strings are equal. Note you should use this function instead of s1==s2, which can (for technical reasons) return false even if the strings are the same.

Parameters:
s1 - first string
s2 - second string
Returns:
true if s1 and s2 are both blank, or have the same content

equalsIgnoreCase

public static boolean equalsIgnoreCase(String s1,
                                       String s2)
Determines whether two strings are equal apart from possible upper/lower case distinctions.

Parameters:
s1 - first string
s2 - second string
Returns:
true if s1 and s2 are both blank, or have the same content apart from case folding

startsWith

public static boolean startsWith(String whole,
                                 String start)
Determines whether a string starts with a certain substring.

Parameters:
whole - the string to test
start - the sequence that may appear at the start of whole
Returns:
true if the first few characters of whole are the same as start

endsWith

public static boolean endsWith(String whole,
                               String end)
Determines whether a string ends with a certain substring.

Parameters:
whole - the string to test
end - the sequence that may appear at the end of whole
Returns:
true if the last few characters of whole are the same as end

contains

public static boolean contains(String whole,
                               String sub)
Determines whether a string contains a given substring.

Parameters:
whole - the string to test
sub - the sequence that may appear within whole
Returns:
true if the sequence sub appears within whole

length

public static int length(String str)
Returns the length of a string in characters.

Parameters:
str - string
Returns:
number of characters in str

split

public static String[] split(String words)
Splits a string into an array of space-separated words. One or more spaces separates each word from the next. Leading and trailing spaces are ignored.

The result is an array of strings, and if you want to use the individual elements you need to use square-bracket indexing, with [0] representing the first object

Parameters:
words - string with embedded spaces delimiting the words
Returns:
array of the separate words; you can extract the individual words from the result using square bracket indexing

split

public static String[] split(String words,
                             String regex)
Splits a string into an array of words separated by a given regular expression.

The result is an array of strings, and if you want to use the individual elements you need to use square-bracket indexing, with [0] representing the first object

Parameters:
words - string with multiple parts
regex - regular expression delimiting the different words in the words parameter
Returns:
array of the separate words; you can extract the individual words from the result using square bracket indexing

matches

public static boolean matches(String str,
                              String regex)
Tests whether a string matches a given regular expression.

Parameters:
str - string to test
regex - regular expression string
Returns:
true if regex matches str anywhere

matchGroup

public static String matchGroup(String str,
                                String regex)
Returns the first grouped expression matched in a string defined by a regular expression. A grouped expression is one enclosed in parentheses.

Parameters:
str - string to match against
regex - regular expression containing a grouped section
Returns:
contents of the matched group (or null, if regex didn't match str)

replaceFirst

public static String replaceFirst(String str,
                                  String regex,
                                  String replacement)
Replaces the first occurrence of a regular expression in a string with a different substring value.

Parameters:
str - string to manipulate
regex - regular expression to match in str
replacement - replacement string
Returns:
same as str, but with the first match (if any) of regex replaced by replacement

replaceAll

public static String replaceAll(String str,
                                String regex,
                                String replacement)
Replaces all occurrences of a regular expression in a string with a different substring value.

Parameters:
str - string to manipulate
regex - regular expression to match in str
replacement - replacement string
Returns:
same as str, but with all matches of regex replaced by replacement

substring

public static String substring(String str,
                               int startIndex)
Returns the last part of a given string. The substring begins with the character at the specified index and extends to the end of this string.

Parameters:
str - the input string
startIndex - the beginning index, inclusive
Returns:
last part of str, omitting the first startIndex characters

substring

public static String substring(String str,
                               int startIndex,
                               int endIndex)
Returns a substring of a given string. The substring begins with the character at startIndex and continues to the character at index endIndex-1 Thus the length of the substring is endIndex-startIndex.

Parameters:
str - the input string
startIndex - the beginning index, inclusive
endIndex - the end index, inclusive
Returns:
substring of str

toUpperCase

public static String toUpperCase(String str)
Returns an uppercased version of a string.

Parameters:
str - input string
Returns:
uppercased version of str

toLowerCase

public static String toLowerCase(String str)
Returns an uppercased version of a string.

Parameters:
str - input string
Returns:
uppercased version of str

trim

public static String trim(String str)
Trims whitespace from both ends of a string.

Parameters:
str - input string
Returns:
str with any spaces trimmed from start and finish

padWithZeros

public static String padWithZeros(long value,
                                  int ndigit)
Takes an integer argument and returns a string representing the same numeric value but padded with leading zeros to a specified length.

Parameters:
value - numeric value to pad
ndigit - the number of digits in the resulting string
Returns:
a string evaluating to the same as value with at least ndigit characters


Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.