Top Description Fields Constructors Methods
org.eclipse.jgit.util

public final Class StringUtils

extends Object
Class Inheritance
Imports
java.text.MessageFormat, java.util.Collection, org.eclipse.jgit.annotations.NonNull, .Nullable, org.eclipse.jgit.internal.JGitText, org.eclipse.jgit.lib.Constants

Miscellaneous string comparison utility methods.

Field Summary

Modifier and TypeField and Description
private static final String
private static final long
private static final long
private static final char[]
private static final long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

the capitalized String, null if null String input
capitalize
(String
the String to capitalize, may be null
str
)

Borrowed from commons-lang StringUtils.capitalize() method.

public static String

Returns:

common prefix of all Strings
commonPrefix
(String...
Strings to consider
strings
)

Compares Strings and returns the initial sequence of characters that is common to all of them.

public static int

Returns:

an int.
compareIgnoreCase
(String
first string to compare.
a
,
String
second string to compare.
b
)

Compare two strings, ignoring case.

public static int

Returns:

an int.
compareWithCase
(String
first string to compare.
a
,
String
second string to compare.
b
)

Compare two strings, honoring case.

public static boolean

Returns:

true if a equals b
equalsIgnoreCase
(String
first string to compare.
a
,
String
second string to compare.
b
)

Test if two strings are equal, ignoring case.

public static String

Returns:

the value's String representation
formatWithSuffix
(long
Value to format
value
)

Formats an integral value as a decimal number with 'k', 'm', or 'g' suffix if it is an exact multiple of 1024, otherwise returns the value representation as a decimal number without suffix.

public static boolean

Returns:

true if the string is null or empty
isEmptyOrNull
(String
the string to check
stringValue
)

Test if a string is empty or null.

public static String

Returns:

a String with all the joined parts
join
(Collection<String>
Strings to join
parts
,
String
used to join
separator
)

Join a collection of Strings together using the specified separator.

public static String

Returns:

a String with all the joined parts
join
(Collection<String>
Strings to join
parts
,
String
separator used to join all but the two last elements
separator
,
String
separator to use for joining the last two elements
lastSeparator
)

Join a collection of Strings together using the specified separator and a lastSeparator which is used for joining the second last and the last part.

public static String

Returns:

the name ending with Constants#DOT_GIT_EXT
nameWithDotGit
(String
to complete
name
)

Appends Constants#DOT_GIT_EXT unless the given name already ends with that suffix.

public static int

Returns:

the value parsed
parseIntWithSuffix
(String
String to parse; with leading and trailing whitespace ignored
value
,
boolean
true to only accept positive numbers, false to allow negative numbers, too
positiveOnly
)

Parses a number with optional case-insensitive suffix 'k', 'm', or 'g' indicating KiB, MiB, and GiB, respectively.

public static long

Returns:

the value parsed
parseLongWithSuffix
(String
String to parse; with leading and trailing whitespace ignored
value
,
boolean
true to only accept positive numbers, false to allow negative numbers, too
positiveOnly
)

Parses a number with optional case-insensitive suffix 'k', 'm', or 'g' indicating KiB, MiB, and GiB, respectively.

public static String

Returns:

in without line breaks
replaceLineBreaksWithSpace
(String
A string with line breaks
in
)

Replace CRLF, CR or LF with a single space.

public static boolean

Returns:

the boolean interpretation of stringValue.
toBoolean
(String
the string to parse.
stringValue
)

Parse a string as a standard Git boolean value.

public static Boolean

Returns:

the boolean interpretation of value or null in case the string does not represent a boolean value
toBooleanOrNull
(String
the string to parse.
stringValue
)

Parse a string as a standard Git boolean value.

public static char

Returns:

lowercase version of the input.
toLowerCase
(char
the input character.
c
)

Convert the input to lowercase.

public static String

Returns:

a copy of the input string, after converting characters in the range 'A'..'Z' to 'a'..'z'.
toLowerCase
(String
the input string. Must not be null.
in
)

Convert the input string to lower case, according to the "C" locale.

public static String

Returns:

the input string with c
trim
(String
input string
str
,
char
character to remove
c
)

Remove the specified character from beginning and end of a string

If the character repeats, all copies

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTYback to summary
private static final String EMPTY
GiBback to summary
private static final long GiB
KiBback to summary
private static final long KiB
LCback to summary
private static final char[] LC
MiBback to summary
private static final long MiB

Constructor Detail

StringUtilsback to summary
private StringUtils()

Method Detail

capitalizeback to summary
public static String capitalize(String str)

Borrowed from commons-lang StringUtils.capitalize() method.

Capitalizes a String changing the first letter to title case as per java.lang.Character#toTitleCase(char). No other letters are changed.

A null input String returns null.

Parameters
str:String

the String to capitalize, may be null

Returns:String

the capitalized String, null if null String input

Since
4.0
commonPrefixback to summary
public static String commonPrefix(String... strings)

Compares Strings and returns the initial sequence of characters that is common to all of them.

Parameters
strings:String[]

Strings to consider

Returns:String

common prefix of all Strings

Annotations
@NonNull
Since
6.8
compareIgnoreCaseback to summary
public static int compareIgnoreCase(String a, String b)

Compare two strings, ignoring case.

This method does not honor the JVM locale, but instead always behaves as though it is in the US-ASCII locale.

Parameters
a:String

first string to compare.

b:String

second string to compare.

Returns:int

an int.

Since
2.0
compareWithCaseback to summary
public static int compareWithCase(String a, String b)

Compare two strings, honoring case.

This method does not honor the JVM locale, but instead always behaves as though it is in the US-ASCII locale.

Parameters
a:String

first string to compare.

b:String

second string to compare.

Returns:int

an int.

Since
2.0
equalsIgnoreCaseback to summary
public static boolean equalsIgnoreCase(String a, String b)

Test if two strings are equal, ignoring case.

This method does not honor the JVM locale, but instead always behaves as though it is in the US-ASCII locale.

Parameters
a:String

first string to compare.

b:String

second string to compare.

Returns:boolean

true if a equals b

formatWithSuffixback to summary
public static String formatWithSuffix(long value)

Formats an integral value as a decimal number with 'k', 'm', or 'g' suffix if it is an exact multiple of 1024, otherwise returns the value representation as a decimal number without suffix.

Parameters
value:long

Value to format

Returns:String

the value's String representation

Since
6.0
isEmptyOrNullback to summary
public static boolean isEmptyOrNull(String stringValue)

Test if a string is empty or null.

Parameters
stringValue:String

the string to check

Returns:boolean

true if the string is null or empty

joinback to summary
public static String join(Collection<String> parts, String separator)

Join a collection of Strings together using the specified separator.

Parameters
parts:Collection<String>

Strings to join

separator:String

used to join

Returns:String

a String with all the joined parts

joinback to summary
public static String join(Collection<String> parts, String separator, String lastSeparator)

Join a collection of Strings together using the specified separator and a lastSeparator which is used for joining the second last and the last part.

Parameters
parts:Collection<String>

Strings to join

separator:String

separator used to join all but the two last elements

lastSeparator:String

separator to use for joining the last two elements

Returns:String

a String with all the joined parts

nameWithDotGitback to summary
public static String nameWithDotGit(String name)

Appends Constants#DOT_GIT_EXT unless the given name already ends with that suffix.

Parameters
name:String

to complete

Returns:String

the name ending with Constants#DOT_GIT_EXT

Since
6.1
parseIntWithSuffixback to summary
public static int parseIntWithSuffix(String value, boolean positiveOnly) throws NumberFormatException, StringIndexOutOfBoundsException

Parses a number with optional case-insensitive suffix 'k', 'm', or 'g' indicating KiB, MiB, and GiB, respectively. The suffix may follow the number with optional separation by blanks.

Parameters
value:String

String to parse; with leading and trailing whitespace ignored

positiveOnly:boolean

true to only accept positive numbers, false to allow negative numbers, too

Returns:int

the value parsed

Exceptions
NumberFormatException:
if the value is not parseable or beyond the range of Integer
StringIndexOutOfBoundsException:
if the string is empty or contains only whitespace, or contains only the letter 'k', 'm', or 'g'
Since
6.0
parseLongWithSuffixback to summary
public static long parseLongWithSuffix(String value, boolean positiveOnly) throws NumberFormatException, StringIndexOutOfBoundsException

Parses a number with optional case-insensitive suffix 'k', 'm', or 'g' indicating KiB, MiB, and GiB, respectively. The suffix may follow the number with optional separation by one or more blanks.

Parameters
value:String

String to parse; with leading and trailing whitespace ignored

positiveOnly:boolean

true to only accept positive numbers, false to allow negative numbers, too

Returns:long

the value parsed

Exceptions
NumberFormatException:
if the value is not parseable, or beyond the range of Long
StringIndexOutOfBoundsException:
if the string is empty or contains only whitespace, or contains only the letter 'k', 'm', or 'g'
Since
6.0
replaceLineBreaksWithSpaceback to summary
public static String replaceLineBreaksWithSpace(String in)

Replace CRLF, CR or LF with a single space.

Parameters
in:String

A string with line breaks

Returns:String

in without line breaks

Since
3.1
toBooleanback to summary
public static boolean toBoolean(String stringValue)

Parse a string as a standard Git boolean value. See toBooleanOrNull(String).

Parameters
stringValue:String

the string to parse.

Returns:boolean

the boolean interpretation of stringValue.

Exceptions
IllegalArgumentException:
if stringValue is not recognized as one of the standard boolean names.
toBooleanOrNullback to summary
public static Boolean toBooleanOrNull(String stringValue)

Parse a string as a standard Git boolean value.

The terms yes, true, 1, on can all be used to mean true.

The terms no, false, 0, off can all be used to mean false.

Comparisons ignore case, via equalsIgnoreCase(String, String).

Parameters
stringValue:String

the string to parse.

Returns:Boolean

the boolean interpretation of value or null in case the string does not represent a boolean value

toLowerCaseback to summary
public static char toLowerCase(char c)

Convert the input to lowercase.

This method does not honor the JVM locale, but instead always behaves as though it is in the US-ASCII locale. Only characters in the range 'A' through 'Z' are converted. All other characters are left as-is, even if they otherwise would have a lowercase character equivalent.

Parameters
c:char

the input character.

Returns:char

lowercase version of the input.

toLowerCaseback to summary
public static String toLowerCase(String in)

Convert the input string to lower case, according to the "C" locale.

This method does not honor the JVM locale, but instead always behaves as though it is in the US-ASCII locale. Only characters in the range 'A' through 'Z' are converted, all other characters are left as-is, even if they otherwise would have a lowercase character equivalent.

Parameters
in:String

the input string. Must not be null.

Returns:String

a copy of the input string, after converting characters in the range 'A'..'Z' to 'a'..'z'.

trimback to summary
public static String trim(String str, char c)

Remove the specified character from beginning and end of a string

If the character repeats, all copies

Parameters
str:String

input string

c:char

character to remove

Returns:String

the input string with c

Since
7.2