public class StringUtil extends Object
Modifier and Type | Method and Description |
---|---|
static boolean |
containsIgnoreCase(String source,
String target)
Returns true if and only if the string
s contains the specified
target string performing case insensitive string comparison. |
static boolean |
equalsIgnoreCase(char ch1,
char ch2)
Returns true if the two characters are equal if case is ignored.
|
static int |
findEndTrimNewlineChars(CharSequence s)
Finds and returns the end of the given character sequence after trimming
new line characters from the right.
|
static int |
findEndTrimWhitespace(CharSequence s)
Finds and returns the end of the given character sequence after trimming
white space characters from the right.
|
static int |
findStartTrimNewlineChars(CharSequence s)
Finds and returns the start of the given sequence after trimming newline
characters from the left.
|
static int |
findStartTrimNewlineChars(CharSequence s,
int start)
Finds and returns the start of the given sequence after trimming newline
characters from the left, starting at the given
start index. |
static int |
findStartTrimWhitespace(CharSequence s)
Finds and returns the start of the given sequence after trimming
whitespace characters from the left.
|
static int |
findStartTrimWhitespace(CharSequence s,
int start)
Finds and returns the start of the given sequence after trimming
whitespace characters from the left, starting at the given
start
index. |
static int |
findWhitespace(CharSequence s)
Finds and returns the first whitespace character in the given sequence,
or the length of the string if no whitespace is found.
|
static int |
findWhitespace(CharSequence s,
int start)
Finds and returns the first whitespace character in the given sequence at
or after start.
|
static String |
fixSizeString(int size,
boolean alignLeft,
char filler,
long value)
Returns the given
value as a string of fixed length size
padding or truncating the value if necessary. |
static String |
fixSizeString(int size,
boolean alignLeft,
char filler,
String s)
Returns the given string
s into a string of fixed length
size padding or truncating the string if necessary. |
static String |
fixSizeString(int size,
boolean alignLeft,
String s)
Returns the given string
s into a string of fixed length
size padding or truncating the string with spaces if necessary. |
static int |
indexOfIgnoreCase(String source,
String target)
Returns the index within the source string of the first occurrence of the
specified target substring performing case insensitive string comparison.
|
static List<Line> |
splitLines(String s)
Splits the given string into lines and returns each line as a separate
string in the result list.
|
static boolean |
startsWithIgnoreCase(String s,
String prefix)
Tests if this string
s starts with the specified prefix
performing case insensitive string comparison. |
public static String fixSizeString(int size, boolean alignLeft, char filler, long value)
value
as a string of fixed length size
padding or truncating the value if necessary.
If left==true
, the given value
is left-aligned appending
the given filler
character to make up the fixed length. If the
given value
turns out to be longer than size
when
transformed into a string, it is truncated from the right.
If left==false
, the given value
is right-aligned and
filler
characters are added from the left if necessary. If
value
is longer than size
it is truncated from the left.
Examples with size=3
:
size
- the fixed size of the returned stringalignLeft
- true if value
should be left-alignedfiller
- the filler character if value
is shorter than
size
value
- the value to formatpublic static String fixSizeString(int size, boolean alignLeft, String s)
s
into a string of fixed length
size
padding or truncating the string with spaces if necessary.
If left==true
, the given string s
is left-aligned
appending spaces to make up the fixed length. If s
turns out to
be longer than size
it is truncated from the right.
If left==false
, the given string s
is right-aligned and
space characters are added from the left if necessary. If s
is
longer than size
it is truncated from the left.
Examples with size=3
:
size
- the fixed size of the returned stringalignLeft
- true if value
should be left-aligneds
- the string to formats
as a fixed size string, padded or truncated
if necessarypublic static String fixSizeString(int size, boolean alignLeft, char filler, String s)
s
into a string of fixed length
size
padding or truncating the string if necessary.
If left==true
, the given string s
is left-aligned
appending the given filler
character to make up the fixed length.
If s
turns out to be longer than size
it is truncated
from the right.
If left==false
, the given string s
is right-aligned and
filler
characters are added from the left if necessary. If
s
is longer than size
it is truncated from the left.
Examples with size=3
:
size
- the fixed size of the returned stringalignLeft
- true if value
should be left-alignedfiller
- the filler character if s
is shorter than size
s
- the string to formats
as a fixed size string, padded or truncated
if necessarypublic static List<Line> splitLines(String s)
A trailing newline after the last line is ignored, meaning that no empty string is appended as separate line if the string ends with a newline. However multiple trailing newlines will still lead to empty line strings at the end of the list.
Note that all line ending characters are accepted to split lines, no
matter what operating system this code is hosted on. More precisely, the
LF
and CR
characters are recognized as
line ending characters, either as single character or as a pair
CR+LF
or LF+CR
.
s
- the string to splits
public static int findStartTrimWhitespace(CharSequence s)
s
- the character sequencepublic static int findStartTrimWhitespace(CharSequence s, int start)
start
index.s
- the character sequencestart
- the first index to consider in the char sequencepublic static int findEndTrimWhitespace(CharSequence s)
Character.isWhitespace(char)
. .s
- the character sequencepublic static int findStartTrimNewlineChars(CharSequence s)
s
- the character sequencepublic static int findStartTrimNewlineChars(CharSequence s, int start)
start
index. .
The following character sequences are treated as newline characters:
"\n", "\r\n".s
- the character sequencestart
- the first index to consider in the char sequencepublic static int findEndTrimNewlineChars(CharSequence s)
s
- the character sequencepublic static int findWhitespace(CharSequence s)
s
- the character sequencepublic static int findWhitespace(CharSequence s, int start)
s
- the character sequencestart
- the first index to consider in the char sequencepublic static boolean containsIgnoreCase(String source, String target)
s
contains the specified
target string performing case insensitive string comparison.source
- the characters being searched.target
- the characters being searched for.s
, false otherwiseNullPointerException
- if s
is null
public static boolean startsWithIgnoreCase(String s, String prefix)
s
starts with the specified prefix
performing case insensitive string comparison.s
- the string to searchprefix
- the prefix.true
if the character sequence represented by the
argument is a prefix of the character sequence represented by the
string s; false
otherwise. Note also that
true
will be returned if the argument is an empty
string or is equal to this String
object as
determined by the Object.equals(Object)
method.public static int indexOfIgnoreCase(String source, String target)
The returned index is the smallest value k for which:
If no such value of k exists, thenstartsWithIgnoreCase(source.substring(k), target)
-1
is
returned.
Copied from String.indexOf(..)
modified to do case-insensitive
search. The source is the character array being searched, and the target
is the string being searched for.
source
- the characters being searched.target
- the characters being searched for.-1
if there is no such
occurrence.public static boolean equalsIgnoreCase(char ch1, char ch2)
ch1
- the first characterch2
- the second characterCopyright © 2024. All rights reserved.