public class Strings
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
concat(java.lang.Object... strings)
Concatenates multiple values into a string.
|
static boolean |
contains(java.lang.String whole,
java.lang.String sub)
Determines whether a string contains a given substring.
|
static boolean |
endsWith(java.lang.String whole,
java.lang.String end)
Determines whether a string ends with a certain substring.
|
static boolean |
equals(java.lang.String s1,
java.lang.String s2)
Determines whether two strings are equal.
|
static boolean |
equalsIgnoreCase(java.lang.String s1,
java.lang.String s2)
Determines whether two strings are equal apart from possible
upper/lower case distinctions.
|
static java.lang.String |
join(java.lang.String separator,
java.lang.Object... words)
Joins multiple values into a string, with a given
separator between each pair.
|
static int |
length(java.lang.String str)
Returns the length of a string in characters.
|
static boolean |
matches(java.lang.String str,
java.lang.String regex)
Tests whether a string matches a given regular expression.
|
static java.lang.String |
matchGroup(java.lang.String str,
java.lang.String regex)
Returns the first grouped expression matched in a string defined
by a regular expression.
|
static java.lang.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 java.lang.String |
replaceAll(java.lang.String str,
java.lang.String regex,
java.lang.String replacement)
Replaces all occurrences of a regular expression in a string with
a different substring value.
|
static java.lang.String |
replaceFirst(java.lang.String str,
java.lang.String regex,
java.lang.String replacement)
Replaces the first occurrence of a regular expression in a string with
a different substring value.
|
static java.lang.String[] |
split(java.lang.String words)
Splits a string into an array of space-separated words.
|
static java.lang.String[] |
split(java.lang.String words,
java.lang.String regex)
Splits a string into an array of words separated by a given
regular expression.
|
static boolean |
startsWith(java.lang.String whole,
java.lang.String start)
Determines whether a string starts with a certain substring.
|
static java.lang.String |
substring(java.lang.String str,
int startIndex)
Returns the last part of a given string.
|
static java.lang.String |
substring(java.lang.String str,
int startIndex,
int endIndex)
Returns a substring of a given string.
|
static java.lang.String |
toLowerCase(java.lang.String str)
Returns an lowercased version of a string.
|
static java.lang.String |
toUpperCase(java.lang.String str)
Returns an uppercased version of a string.
|
static java.lang.String |
trim(java.lang.String str)
Trims whitespace from both ends of a string.
|
public static java.lang.String concat(java.lang.Object... strings)
s1+s2+...
, but this method makes sure that
values are converted to strings, with the blank value invisible.strings
- one or more stringsconcat("blue", "moon") = "bluemoon"
, concat("1", 2, 3, "4") = "1234"
, concat("Astro", null, "Physics") = "AstroPhysics"
public static java.lang.String join(java.lang.String separator, java.lang.Object... words)
separator
- string to insert between adjacent wordswords
- one or more values to joinseparator
join("<->", "alpha", "beta", "gamma")
= "alpha<->beta<->gamma"
, join(" ", 1, "brown", "mouse")
= "1 brown mouse"
public static boolean equals(java.lang.String s1, java.lang.String s2)
s1==s2
,
which can (for technical reasons) return false even if the
strings are the same.s1
- first strings2
- second stringpublic static boolean equalsIgnoreCase(java.lang.String s1, java.lang.String s2)
s1
- first strings2
- second stringequalsIgnoreCase("Cygnus", "CYGNUS") = true
, equalsIgnoreCase("Cygnus", "Andromeda") = false
public static boolean startsWith(java.lang.String whole, java.lang.String start)
whole
- the string to teststart
- the sequence that may appear at the start of
whole
whole
are
the same as start
startsWith("CYGNUS X-1", "CYG") = true
public static boolean endsWith(java.lang.String whole, java.lang.String end)
whole
- the string to testend
- the sequence that may appear at the end of
whole
whole
are
the same as end
endsWith("M32", "32") = true
public static boolean contains(java.lang.String whole, java.lang.String sub)
whole
- the string to testsub
- the sequence that may appear within whole
sub
appears within
whole
contains("Vizier", "izi") = true
public static int length(java.lang.String str)
str
- stringstr
length("M34") = 3
public static java.lang.String[] split(java.lang.String words)
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
words
- string with embedded spaces delimiting the wordssplit("211:54:01 +29:33:41")
gives a 2-element array,
first element is "211:54:01"
and
second element is "+29:33:41"
., split(" cat dog cow ")[1] = "dog"
public static java.lang.String[] split(java.lang.String words, java.lang.String regex)
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
words
- string with multiple partsregex
- regular expression delimiting the different words in
the words
parametersplit("cat, dog, cow", ", *")
gives a 3-element string array., split("23.0, 45.92", ", ")[0] = "23.0"
, parseDouble(split("23.0, 45.92", ", ")[0]) = 23
public static boolean matches(java.lang.String str, java.lang.String regex)
str
- string to testregex
- regular expression stringregex
matches str
anywherematches("Hubble", "ub") = true
public static java.lang.String matchGroup(java.lang.String str, java.lang.String regex)
str
- string to match againstregex
- regular expression containing a grouped sectionregex
didn't match str
)matchGroup("NGC28948b","NGC([0-9]*)") = "28948"
public static java.lang.String replaceFirst(java.lang.String str, java.lang.String regex, java.lang.String replacement)
str
- string to manipulateregex
- regular expression to match in str
replacement
- replacement stringstr
, but with the first match (if any) of
regex
replaced by replacement
replaceFirst("Messier 61", "Messier ", "M-") = "M-61"
public static java.lang.String replaceAll(java.lang.String str, java.lang.String regex, java.lang.String replacement)
str
- string to manipulateregex
- regular expression to match in str
replacement
- replacement stringstr
, but with all matches of
regex
replaced by replacement
replaceAll("1-2--3---4","--*","x") = "1x2x3x4"
public static java.lang.String substring(java.lang.String str, int startIndex)
str
- the input stringstartIndex
- the beginning index, inclusivestr
, omitting the first
startIndex
characterssubstring("Galaxy", 2) = "laxy"
public static java.lang.String substring(java.lang.String str, int startIndex, int endIndex)
startIndex
and continues to the character at index endIndex-1
Thus the length of the substring is endIndex-startIndex
.str
- the input stringstartIndex
- the beginning index, inclusiveendIndex
- the end index, inclusivestr
substring("Galaxy", 2, 5) = "lax"
public static java.lang.String toUpperCase(java.lang.String str)
str
- input stringstr
toUpperCase("Universe") = "UNIVERSE"
public static java.lang.String toLowerCase(java.lang.String str)
str
- input stringstr
toLowerCase("Universe") = "universe"
public static java.lang.String trim(java.lang.String str)
str
- input stringtrim(" some text ") = "some text"
, trim("some text") = "some text"
public static java.lang.String padWithZeros(long value, int ndigit)
value
- numeric value to padndigit
- the number of digits in the resulting stringvalue
with
at least ndigit
characterspadWithZeros(23,5) = "00023"
Copyright © 2017 Central Laboratory of the Research Councils. All Rights Reserved.