|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectStatic
Strings
public final class Strings
Utility methods working on String or CharSequence instances. Some methods
defined in this class duplicate the functionalities already provided in the String
class, but works on a generic CharSequence instance instead than String.
Other methods perform their work directly on the provided StringBuilder.
Unicode support
Every methods defined in this class work on code points instead than characters
when appropriate. Consequently those methods should behave correctly with characters outside
the Basic Multilingual Plane (BMP).
Handling of null values
Some methods accept a null argument, in particular the methods converting the
given String to another String which may be the same. For example the
camelCaseToAcronym(String) method returns null if the string to convert is
null. Some other methods like count(String, char) handles null
argument like an empty string. The methods that don't accept a null argument are
explicitly documented as throwing a NullPointerException.
Arrays.toString(Object[]),
XArrays.containsIgnoreCase(String[], String)
| utility/geotk-utility (download) | View source code for this class |
| Field Summary | |
|---|---|
static String[] |
EMPTY
An array of zero-length. |
| Method Summary | |
|---|---|
static String |
camelCaseToAcronym(String text)
Creates an acronym from the given text. |
static String |
camelCaseToSentence(CharSequence identifier)
Given a string in camel cases (typically a Java identifier), returns a string formatted like an English sentence. |
static StringBuilder |
camelCaseToWords(CharSequence identifier,
boolean toLowerCase)
Given a string in camel cases, returns a string with the same words separated by spaces. |
static String |
commonPrefix(String s1,
String s2)
Returns the longest sequence of characters which is found at the beginning of the two given strings. |
static String |
commonSuffix(String s1,
String s2)
Returns the longest sequence of characters which is found at the end of the two given strings. |
static int |
count(CharSequence text,
char c)
Counts the number of occurrence of the given character in the given character sequence. |
static int |
count(String text,
char c)
Counts the number of occurrence of the given character in the given string. |
static int |
count(String text,
String toSearch)
Returns the number of occurrences of the toSearch string in the given text. |
static boolean |
endsWith(CharSequence sequence,
CharSequence suffix,
boolean ignoreCase)
Returns true if the given character sequence ends with the given suffix. |
static boolean |
equalsIgnoreCase(CharSequence s1,
CharSequence s2)
Returns true if the two given strings are equal, ignoring case. |
static String[] |
getLinesFromMultilines(String text)
Returns a String instance for each line found in a multi-lines string. |
static int |
indexOf(CharSequence string,
CharSequence part,
int fromIndex)
Returns the index within the given strings of the first occurrence of the specified part, starting at the specified index. |
static boolean |
isAcronymForWords(CharSequence acronym,
CharSequence words)
Returns true if the first string is likely to be an acronym of the second string. |
static boolean |
isJavaIdentifier(CharSequence identifier)
Returns true if the given identifier is a legal Java identifier. |
static boolean |
isUpperCase(CharSequence text)
Returns true if every characters in the given character sequence are
upper-case. |
static byte[] |
parseBytes(String values,
char separator,
int radix)
Splits the given string around the given character, then parses each item as a byte. |
static double[] |
parseDoubles(String values,
char separator)
Splits the given string around the given character, then parses each item as a double. |
static float[] |
parseFloats(String values,
char separator)
Splits the given string around the given character, then parses each item as a float. |
static int[] |
parseInts(String values,
char separator,
int radix)
Splits the given string around the given character, then parses each item as an int. |
static long[] |
parseLongs(String values,
char separator,
int radix)
Splits the given string around the given character, then parses each item as a long. |
static short[] |
parseShorts(String values,
char separator,
int radix)
Splits the given string around the given character, then parses each item as a short. |
static boolean |
regionMatches(CharSequence string,
int offset,
CharSequence part)
Returns true if the given string at the given offset contains the given part,
in a case-sensitive comparison. |
static void |
remove(StringBuilder buffer,
String search)
Removes every occurrences of the given string in the given buffer. |
static void |
replace(StringBuilder buffer,
int start,
int end,
char[] chars)
Replaces the characters in a substring of the buffer with characters in the specified array. |
static void |
replace(StringBuilder buffer,
String search,
String replacement)
Replaces every occurrences of the given string in the given buffer. |
static int |
skipLines(CharSequence string,
int numToSkip,
int startAt)
Returns the index of the first character after the given number of lines. |
static String |
spaces(int length)
Returns a string of the specified length filled with white spaces. |
static String[] |
split(String toSplit,
char separator)
Splits a string around the given character. |
static boolean |
startsWith(CharSequence sequence,
CharSequence prefix,
boolean ignoreCase)
Returns true if the given character sequence starts with the given prefix. |
static CharSequence |
toASCII(CharSequence text)
Replaces some Unicode characters by ASCII characters on a "best effort basis". |
static CharSequence |
token(CharSequence text,
int offset)
Returns the token starting at the given offset in the given text. |
static String |
trim(String text)
Returns a string with leading and trailing white spaces omitted. |
static String |
trimFractionalPart(String value)
Trims the fractional part of the given formatted number, provided that it doesn't change the value. |
static void |
trimFractionalPart(StringBuilder buffer)
Trims the fractional part of the given formatted number, provided that it doesn't change the value. |
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String[] EMPTY
Collections.EMPTY_LIST.
| Method Detail |
|---|
public static String spaces(int length)
length - The string length. Negative values are clamped to 0.
length filled with white spaces.
public static int count(String text,
String toSearch)
toSearch string in the given text.
The search is case-sensitive.
text - String to search in, or null.toSearch - The string to search in the given text.
Must contain at least one character.
toSearch in text,
or 0 if text was null or empty.
IllegalArgumentException - If the toSearch array is null or empty.
public static int count(String text,
char c)
count(CharSequence, char), but is faster.
text - The text in which to count the number of occurrence.c - The character to count, or 0 if text was null.
public static int count(CharSequence text,
char c)
count(String, char), but on a more
generic interface.
text - The text in which to count the number of occurrence.c - The character to count, or 0 if text was null.
public static String[] split(String toSplit,
char separator)
This method is similar to the standard String.split(String) method except for the
following:
null input string, in which case an empty array is returned.
toSplit - The string to split, or null.separator - The delimiting character (typically the coma).
toSplit was null.String.split(String)
public static double[] parseDoubles(String values,
char separator)
throws NumberFormatException
double.
values - The strings containing the values to parse, or null.separator - The delimiting character (typically the coma).
values was null.
NumberFormatException - If at least one number can not be parsed.
public static float[] parseFloats(String values,
char separator)
throws NumberFormatException
float.
values - The strings containing the values to parse, or null.separator - The delimiting character (typically the coma).
values was null.
NumberFormatException - If at least one number can not be parsed.
public static long[] parseLongs(String values,
char separator,
int radix)
throws NumberFormatException
long.
values - The strings containing the values to parse, or null.separator - The delimiting character (typically the coma).radix - the radix to be used for parsing. This is usually 10.
values was null.
NumberFormatException - If at least one number can not be parsed.
public static int[] parseInts(String values,
char separator,
int radix)
throws NumberFormatException
int.
values - The strings containing the values to parse, or null.separator - The delimiting character (typically the coma).radix - the radix to be used for parsing. This is usually 10.
values was null.
NumberFormatException - If at least one number can not be parsed.
public static short[] parseShorts(String values,
char separator,
int radix)
throws NumberFormatException
short.
values - The strings containing the values to parse, or null.separator - The delimiting character (typically the coma).radix - the radix to be used for parsing. This is usually 10.
values was null.
NumberFormatException - If at least one number can not be parsed.
public static byte[] parseBytes(String values,
char separator,
int radix)
throws NumberFormatException
byte.
values - The strings containing the values to parse, or null.separator - The delimiting character (typically the coma).radix - the radix to be used for parsing. This is usually 10.
values was null.
NumberFormatException - If at least one number can not be parsed.
public static void replace(StringBuilder buffer,
String search,
String replacement)
StringBuilder.replace(int, int, String)
for each occurrence of search found in the buffer.
buffer - The string in which to perform the replacements.search - The string to replace.replacement - The replacement for the target string.
NullPointerException - if any of the arguments is null.String.replace(char, char),
String.replace(CharSequence, CharSequence),
StringBuilder.replace(int, int, String)
public static void replace(StringBuilder buffer,
int start,
int end,
char[] chars)
start and extends to the
character at index end - 1.
buffer - The buffer in which to perform the replacement.start - The beginning index in the buffer, inclusive.end - The ending index in the buffer, exclusive.chars - The array that will replace previous contents.
NullPointerException - if the buffer or chars argument is null.StringBuilder.replace(int, int, String)
public static void remove(StringBuilder buffer,
String search)
StringBuilder.delete(int, int) for each occurrence of search found in
the buffer.
buffer - The string in which to perform the removals.search - The string to remove.
NullPointerException - if any of the arguments is null.StringBuilder.delete(int, int)public static String trim(String text)
Character.isWhitespace(int) method.
This method is similar in purpose to String.trim(), except that the later considers
every ASCII control codes below 32 to be a whitespace. This have the effect of removing
X3.64 escape sequences as well. Users should invoke
this Strings.trim method instead if they need to preserve X3.64 escape sequences.
text - The string from which to remove leading and trailing white spaces, or null.
null is the given
string was null.String.trim()public static String trimFractionalPart(String value)
Double.toString(double) method.
More specifically if the given string ends with a '.' character followed by a
sequence of '0' characters, then those characters are omitted. Otherwise this
method returns the string unchanged. This is a "all or nothing" method:
either the fractional part is completely removed, or either it is left unchanged.
Examples
This method returns "4" if the given value is "4.", "4.0" or
"4.00", but returns "4.10" unchanged (including the trailing '0'
character) if the input is "4.10".
Use case
This method is useful before to parse a number
if that number should preferably be parsed as an integer before attempting to parse
it as a floating point number.
value - The value to trim if possible, or null.
".0" part (if any),
or null if the given string was null.public static void trimFractionalPart(StringBuilder buffer)
trimFractionalPart(String)
except that it modifies the given buffer in-place.
Use case
This method is useful after a double value has
been appended to the buffer, in order to make it appears like an integer when possible.
buffer - The buffer to trim if possible.
NullPointerException - if the argument is null.public static CharSequence toASCII(CharSequence text)
'é' character is replaced by 'e' (without accent).
The current implementation replaces only the characters in the range 00C0
to 00FF, inclusive. Other characters are left unchanged.
Note that if the given character sequence is an instance of StringBuilder,
then the replacement will be performed in-place.
text - The text to scan for Unicode characters to replace by ASCII characters,
or null.
text if no replacement
has been applied.public static String camelCaseToSentence(CharSequence identifier)
Invoke camelCaseToWords(CharSequence, boolean), which separate the words
on the basis of character case. For example "transferFunctionType" become
"transfer function type". This works fine for ISO 19115 identifiers.
Next replace all occurrence of '_' by spaces in order to take in account
an other common naming convention, which uses '_' as a word separator. This
convention is used by NetCDF attributes like "project_name".
Finally ensure that the first character is upper-case.
Exception to the above rules
If the given identifier contains only upper-case letters, digits and the '_'
character, then the identifier is returned "as is" except for the '_' characters
which are replaced by '-'. This work well for identifiers like "UTF-8" or
"ISO-LATIN-1" for example.
Note that those heuristic rules may be modified in future Geotk versions, depending on the practical experience gained.
identifier - An identifier with no space, words begin with an upper-case character,
or null.
null
if the given argument was null.
public static StringBuilder camelCaseToWords(CharSequence identifier,
boolean toLowerCase)
"PixelInterleavedSampleModel", then this method returns
"Pixel Interleaved Sample Model" or "Pixel interleaved sample model"
depending on the value of the toLowerCase argument.
If toLowerCase is false, then this method inserts spaces but does not change
the case of characters. If toLowerCase is true, then this method changes
to lower case the first character after each spaces
inserted by this method (note that this intentionally exclude the very first character in
the given string), except if the second character is upper case, in which case the words is assumed an acronym.
The given string is usually a programmatic identifier like a class name or a method name.
identifier - An identifier with no space, words begin with an upper-case character.toLowerCase - true for changing the first character of words to lower case,
except for the first word and acronyms.
StringBuilder in order to allow modifications by the caller.
NullPointerException - if the identifier argument is null.public static String camelCaseToAcronym(String text)
'_' character, or any
character which is not a java identifier
part (including spaces).
Examples: given "northEast", this method returns "NE".
Given "Open Geospatial Consortium", this method returns "OGC".
text - The text for which to create an acronym, or null.
null if the given text was null.
public static boolean isAcronymForWords(CharSequence acronym,
CharSequence words)
true if the first string is likely to be an acronym of the second string.
An acronym is a sequence of letters or digits
built from at least one character of each word in the words string. More than
one character from the same word may appear in the acronym, but they must always
be the first consecutive characters. The comparison is case-insensitive.
Example: given the string "Open Geospatial Consortium", the following
strings are recognized as acronyms: "OGC", "ogc", "O.G.C.",
"OpGeoCon".
acronym - A possible acronym of the sequence of words.words - The sequence of words.
true if the first string is an acronym of the second one.
NullPointerException - if any of the arguments is null.public static boolean isJavaIdentifier(CharSequence identifier)
true if the given identifier is a legal Java identifier.
This method returns true if the identifier length is greater than zero,
the first character is a Java
identifier start and all remaining characters (if any) are
Java identifier parts.
identifier - The character sequence to test.
true if the given character sequence is a legal Java identifier.
NullPointerException - if the argument is null.public static boolean isUpperCase(CharSequence text)
true if every characters in the given character sequence are
upper-case.
text - The character sequence to test.
true if every character are upper-case.
NullPointerException - if the argument is null.String.toUpperCase()
public static boolean equalsIgnoreCase(CharSequence s1,
CharSequence s2)
true if the two given strings are equal, ignoring case.
This method is similar to String.equalsIgnoreCase(String), except
it works on arbitrary character sequences and compares code points
instead than characters.
s1 - The first string to compare.s2 - The second string to compare.
true if the two given strings are equal, ignoring case.
NullPointerException - if any of the arguments is null.String.equalsIgnoreCase(String)
public static boolean regionMatches(CharSequence string,
int offset,
CharSequence part)
true if the given string at the given offset contains the given part,
in a case-sensitive comparison. This method is equivalent to the following code:
Except that this method works on arbitraryreturn string.regionMatches(offset, part, 0, part.length());
CharSequence objects instead than
Strings only.
string - The string for which to tests for the presence of part.offset - The offset in string where to test for the presence of part.part - The part which may be present in string.
true if string contains part at the given offset.
NullPointerException - if any of the arguments is null.String.regionMatches(int, String, int, int)
public static int indexOf(CharSequence string,
CharSequence part,
int fromIndex)
Except that this method works on arbitraryreturn string.indexOf(part, fromIndex);
CharSequence objects instead than
Strings only.
string - The string in which to perform the search.part - The substring for which to search.fromIndex - The index from which to start the search.
NullPointerException - if any of the arguments is null.String.indexOf(String, int),
StringBuilder.indexOf(String, int),
StringBuffer.indexOf(String, int)
public static CharSequence token(CharSequence text,
int offset)
Let define c as the first non-blank character located at an index equals or greater than the given offset. Then the characters that are considered of the same type are:
Character.getType(int) returns
the same value than for c.
text - The text for which to get the token.offset - Index of the fist character to consider in the given text.
text starting at the given offset, or an empty string
if there is no non-blank character at or after the given offset.
NullPointerException - if the text argument is null.
public static String commonPrefix(String s1,
String s2)
null, then the other string is
returned.
s1 - The first string, or null.s2 - The second string, or null.
null if both strings are null.
public static String commonSuffix(String s1,
String s2)
null, then the other string is returned.
s1 - The first string, or null.s2 - The second string, or null.
null if both strings are null.
public static boolean startsWith(CharSequence sequence,
CharSequence prefix,
boolean ignoreCase)
true if the given character sequence starts with the given prefix.
sequence - The sequence to test.prefix - The expected prefix.ignoreCase - true if the case should be ignored.
true if the given sequence starts with the given prefix.
NullPointerException - if any of the arguments is null.
public static boolean endsWith(CharSequence sequence,
CharSequence suffix,
boolean ignoreCase)
true if the given character sequence ends with the given suffix.
sequence - The sequence to test.suffix - The expected suffix.ignoreCase - true if the case should be ignored.
true if the given sequence ends with the given suffix.
NullPointerException - if any of the arguments is null.
public static int skipLines(CharSequence string,
int numToSkip,
int startAt)
'\n', '\r'
or "\r\n" starting from the given position. When numToSkip
occurrences have been found, the index of the first character after the last
occurrence is returned.
string - The string in which to skip a determined amount of lines.numToSkip - The number of lines to skip. Can be positive, zero or negative.startAt - Index at which to start the search.
NullPointerException - if the string argument is null.public static String[] getLinesFromMultilines(String text)
String instance for each line found in a multi-lines string. Each element
in the returned array will be a single line. If the given text is already a single line,
then this method returns a singleton containing only the given text.
text - The multi-line text from which to get the individual lines.
null if the given text was null.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||