Top Description Inners Fields Constructors Methods
org.eclipse.jetty.util

public Class StringUtil

extends Object
Class Inheritance
Imports
java.io.UnsupportedEncodingException, java.nio.charset.Charset, .StandardCharsets, java.util.ArrayList, .List, .Objects, java.util.concurrent.ThreadLocalRandom, java.util.stream.IntStream

Fast String Utilities.

These string utilities provide both convenience methods and performance improvements over most standard library versions. The main aim of the optimizations is to avoid object creation unless absolutely required.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum

Field Summary

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

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
append(StringBuilder
StringBuilder to append to
buf
,
String
String to append from
s
,
int
The offset of the substring
offset
,
int
The length of the substring
length
)

Append substring to StringBuilder

public static void
append(StringBuilder
the buffer to append to
buf
,
byte
the byte to append
b
,
int
the base of the hex output (almost always 16).
base
)

append hex digit

public static void
append2digits(StringBuilder
the buffer to append to
buf
,
int
the value to append
i
)

Append 2 digits (zero padded) to the StringBuilder

public static String[]

Returns:

An array of parsed values.
arrayFromString
(String
The string to parse, expected to be enclosed as '[...]'
s
)

Parse the string representation of a list using csvSplit(List, String, int, int)

public static boolean

Returns:

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.
asciiEndsWithIgnoreCase
(String
The string to check
string
,
String
The sub string to look for as a suffix
suffix
)

Check for a string suffix, ignoring StandardCharsets#US_ASCII case differences.

public static boolean

Returns:

true if the strings are equal, ignoring StandardCharsets#US_ASCII case differences.
asciiEqualsIgnoreCase
(String
The string to check
string
,
String
The other string to check
other
)

Check for string equality, ignoring StandardCharsets#US_ASCII case differences.

public static boolean

Returns:

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.
asciiStartsWithIgnoreCase
(String
The string to check
string
,
String
The sub string to look for as a prefix
prefix
)

Check for a string prefix, ignoring StandardCharsets#US_ASCII case differences.

public static char

Returns:

a lower case version of c
asciiToLowerCase
(char
the char to convert
c
)

fast lower case conversion.

public static byte

Returns:

a lower case version of c
asciiToLowerCase
(byte
the byte to convert
c
)

fast lower case conversion.

public static String

Returns:

a lower case version of s
asciiToLowerCase
(String
the string to convert
s
)

fast lower case conversion.

public static String

Returns:

a lower case version of s
asciiToUpperCase
(String
the string to convert
s
)

fast upper case conversion.

public static String[]

Returns:

An array of parsed values.
csvSplit
(String
The string to parse
s
)

Parse a CSV string using csvSplit(List, String, int, int)

public static String[]

Returns:

An array of parsed values.
csvSplit
(String
The string to parse
s
,
int
The offset into the string to start parsing
off
,
int
The len in characters to parse
len
)

Parse a CSV string using csvSplit(List, String, int, int)

public static List<String>

Returns:

list containing the parsed list values
csvSplit
(List<String>
The Collection to split to (or null to get a new list)
list
,
String
The string to parse
s
,
int
The offset into the string to start parsing
off
,
int
The len in characters to parse
len
)

Split a quoted comma separated string to a list

Handle rfc4180-like CSV strings, with the exceptions:

  • quoted values may contain double quotes escaped with back-slash
  • Non-quoted values are trimmed of leading trailing white space
  • trailing commas are ignored
  • double commas result in a empty string value
public static boolean

Returns:

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.
endsWithIgnoreCase
(String
The string to check
string
,
String
The sub string to look for as a suffix
suffix
)

Check for a string suffix, ignoring StandardCharsets#US_ASCII case differences.

public static boolean
equals(String s, char[] buf, int offset, int length)

public static byte[]
public static byte[]
public static byte[]
getBytes(String s, String charset)

public static int

Returns:

the length of the string.
getLength
(String
the string.
s
)

Get the length of a string where a null string is length 0.

public static byte[]
public static int

Returns:

the index of the character in the input stream found.
indexFrom
(String
the input string to search
s
,
String
the chars to look for
chars
)

returns the next index of a character from the chars string

public static int

Returns:

the index of first control character in string, -1 if no control characters encountered
indexOfControlChars
(String
the string to test.
str
)

Find the index of a control characters in String

This will return a result on the first occurrence of a control character, regardless if there are more than one.

public static boolean

Returns:

true if string is null or only whitespace characters, false if non-whitespace characters encountered.
isBlank
(String
the string to test.
str
)

Test if a string is null or only has whitespace characters in it.

public static boolean

Returns:

true if string is null or empty.
isEmpty
(String
the string to test.
str
)

Checks if a String is empty ("") or null.

public static boolean
isHex(String str, int offset, int length)

public static boolean

Returns:

true if string is not null and has at least 1 non-whitespace character, false if null or all-whitespace characters.
isNotBlank
(String
the string to test.
str
)

Test if a string is not null and contains at least 1 non-whitespace characters in it.

public static String

Returns:

The string passed in or empty string if it is null.
nonNull
(String
String
s
)

Return a non null string.

public static String
public static String
printable(byte[] b)

public static String
randomAlphaNumeric(int digits)

public static String

Returns:

the now replaced string
replace
(String
the input string
str
,
char
the char to look for
find
,
char
the char to replace with
with
)

Replace chars within string.

public static String

Returns:

the now replaced string
replace
(String
the input string
s
,
String
the string to look for
sub
,
String
the string to replace with
with
)

Replace substrings within string.

public static String

Returns:

the replaced string
replaceFirst
(String
the original string
original
,
String
the target string to look for
target
,
String
the replacement string to use
replacement
)

Replace first substrings within string.

public static String

Returns:

the sanitized output string. or null if str is null.
sanitizeFileSystemName
(String
the raw input string
str
)

Replace all characters from input string that are known to have special meaning in various filesystems.

public static String
public static boolean

Returns:

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.
startsWithIgnoreCase
(String
The string to check
string
,
String
The sub string to look for as a prefix
prefix
)

Check for a string prefix, ignoring StandardCharsets#US_ASCII case differences.

public static String
strip(String str, String find)

public static String
toHexString(byte b)

public static String
toHexString(byte[] b)

public static String
toHexString(byte[] b, int offset, int length)

public static int

Returns:

an int
toInt
(String
A String containing an integer.
string
,
int
The index to start parsing from
from
)

Convert String to an integer.

public static String
toString(byte[] b, int offset, int length, String charset)
Deprecated for removal since 10. use String#String(byte[], int, int, Charset) instead

public static String
toUTF8String(byte[] b, int offset, int length)

public static String

Returns:

the truncated string. if str param is null, then the returned string will also be null.
truncate
(String
the string to possibly truncate
str
,
int
the maximum size of the string
maxSize
)

Truncate a string to a max size.

public static String

Returns:

String value or null
valueOf
(Object
The object
object
)

The String value of an Object

This method calls String#valueOf(Object) unless the object is null, in which case null is returned

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CRLFback to summary
public static final String CRLF
DEFAULT_DELIMSback to summary
public static final String DEFAULT_DELIMS
LOWERCASESback to summary
private static final char[] LOWERCASES
UPPERCASESback to summary
private static final char[] UPPERCASES

Constructor Detail

StringUtilback to summary
private StringUtil()

Method Detail

appendback to summary
public static void append(StringBuilder buf, String s, int offset, int length)

Append substring to StringBuilder

Parameters
buf:StringBuilder

StringBuilder to append to

s:String

String to append from

offset:int

The offset of the substring

length:int

The length of the substring

appendback to summary
public static void append(StringBuilder buf, byte b, int base)

append hex digit

Parameters
buf:StringBuilder

the buffer to append to

b:byte

the byte to append

base:int

the base of the hex output (almost always 16).

append2digitsback to summary
public static void append2digits(StringBuilder buf, int i)

Append 2 digits (zero padded) to the StringBuilder

Parameters
buf:StringBuilder

the buffer to append to

i:int

the value to append

arrayFromStringback to summary
public static String[] arrayFromString(String s)

Parse the string representation of a list using csvSplit(List, String, int, int)

Parameters
s:String

The string to parse, expected to be enclosed as '[...]'

Returns:String[]

An array of parsed values.

asciiEndsWithIgnoreCaseback to summary
public static boolean asciiEndsWithIgnoreCase(String string, String suffix)

Check for a string suffix, ignoring StandardCharsets#US_ASCII case differences.

Parameters
string:String

The string to check

suffix:String

The sub string to look for as a suffix

Returns:boolean

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.

asciiEqualsIgnoreCaseback to summary
public static boolean asciiEqualsIgnoreCase(String string, String other)

Check for string equality, ignoring StandardCharsets#US_ASCII case differences.

Parameters
string:String

The string to check

other:String

The other string to check

Returns:boolean

true if the strings are equal, ignoring StandardCharsets#US_ASCII case differences.

asciiStartsWithIgnoreCaseback to summary
public static boolean asciiStartsWithIgnoreCase(String string, String prefix)

Check for a string prefix, ignoring StandardCharsets#US_ASCII case differences.

Parameters
string:String

The string to check

prefix:String

The sub string to look for as a prefix

Returns:boolean

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.

asciiToLowerCaseback to summary
public static char asciiToLowerCase(char c)

fast lower case conversion. Only works on ascii (not unicode)

Parameters
c:char

the char to convert

Returns:char

a lower case version of c

asciiToLowerCaseback to summary
public static byte asciiToLowerCase(byte c)

fast lower case conversion. Only works on ascii (not unicode)

Parameters
c:byte

the byte to convert

Returns:byte

a lower case version of c

asciiToLowerCaseback to summary
public static String asciiToLowerCase(String s)

fast lower case conversion. Only works on ascii (not unicode)

Parameters
s:String

the string to convert

Returns:String

a lower case version of s

asciiToUpperCaseback to summary
public static String asciiToUpperCase(String s)

fast upper case conversion. Only works on ascii (not unicode)

Parameters
s:String

the string to convert

Returns:String

a lower case version of s

csvSplitback to summary
public static String[] csvSplit(String s)

Parse a CSV string using csvSplit(List, String, int, int)

Parameters
s:String

The string to parse

Returns:String[]

An array of parsed values.

csvSplitback to summary
public static String[] csvSplit(String s, int off, int len)

Parse a CSV string using csvSplit(List, String, int, int)

Parameters
s:String

The string to parse

off:int

The offset into the string to start parsing

len:int

The len in characters to parse

Returns:String[]

An array of parsed values.

csvSplitback to summary
public static List<String> csvSplit(List<String> list, String s, int off, int len)

Split a quoted comma separated string to a list

Handle rfc4180-like CSV strings, with the exceptions:

  • quoted values may contain double quotes escaped with back-slash
  • Non-quoted values are trimmed of leading trailing white space
  • trailing commas are ignored
  • double commas result in a empty string value
Parameters
list:List<String>

The Collection to split to (or null to get a new list)

s:String

The string to parse

off:int

The offset into the string to start parsing

len:int

The len in characters to parse

Returns:List<String>

list containing the parsed list values

endsWithIgnoreCaseback to summary
public static boolean endsWithIgnoreCase(String string, String suffix)

Check for a string suffix, ignoring StandardCharsets#US_ASCII case differences.

Parameters
string:String

The string to check

suffix:String

The sub string to look for as a suffix

Returns:boolean

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.

Annotations
@Deprecated
equalsback to summary
public static boolean equals(String s, char[] buf, int offset, int length)
fromHexStringback to summary
public static byte[] fromHexString(String s)
getBytesback to summary
public static byte[] getBytes(String s)
getBytesback to summary
public static byte[] getBytes(String s, String charset)
getLengthback to summary
public static int getLength(String s)

Get the length of a string where a null string is length 0.

Parameters
s:String

the string.

Returns:int

the length of the string.

getUtf8Bytesback to summary
public static byte[] getUtf8Bytes(String s)
indexFromback to summary
public static int indexFrom(String s, String chars)

returns the next index of a character from the chars string

Parameters
s:String

the input string to search

chars:String

the chars to look for

Returns:int

the index of the character in the input stream found.

indexOfControlCharsback to summary
public static int indexOfControlChars(String str)

Find the index of a control characters in String

This will return a result on the first occurrence of a control character, regardless if there are more than one.

Note

uses codepoint version of Character#isISOControl(int) to support Unicode better.

	   indexOfControlChars(null)      == -1
	   indexOfControlChars("")        == -1
	   indexOfControlChars("\r\n")    == 0
	   indexOfControlChars("\t")      == 0
	   indexOfControlChars("   ")     == -1
	   indexOfControlChars("a")       == -1
	   indexOfControlChars(".")       == -1
	   indexOfControlChars(";\n")     == 1
	   indexOfControlChars("abc\f")   == 3
	   indexOfControlChars("z\010")   == 1
	   indexOfControlChars(":
") == 1
	 
Parameters
str:String

the string to test.

Returns:int

the index of first control character in string, -1 if no control characters encountered

isBlankback to summary
public static boolean isBlank(String str)

Test if a string is null or only has whitespace characters in it.

Note

uses codepoint version of Character#isWhitespace(int) to support Unicode better.

  isBlank(null)   == true
  isBlank("")     == true
  isBlank("\r\n") == true
  isBlank("\t")   == true
  isBlank("   ")  == true
  isBlank("a")    == false
  isBlank(".")    == false
  isBlank(";\n")  == false
Parameters
str:String

the string to test.

Returns:boolean

true if string is null or only whitespace characters, false if non-whitespace characters encountered.

isEmptyback to summary
public static boolean isEmpty(String str)

Checks if a String is empty ("") or null.

  isEmpty(null)   == true
  isEmpty("")     == true
  isEmpty("\r\n") == false
  isEmpty("\t")   == false
  isEmpty("   ")  == false
  isEmpty("a")    == false
  isEmpty(".")    == false
  isEmpty(";\n")  == false
Parameters
str:String

the string to test.

Returns:boolean

true if string is null or empty.

isHexback to summary
public static boolean isHex(String str, int offset, int length)
isNotBlankback to summary
public static boolean isNotBlank(String str)

Test if a string is not null and contains at least 1 non-whitespace characters in it.

Note

uses codepoint version of Character#isWhitespace(int) to support Unicode better.

  isNotBlank(null)   == false
  isNotBlank("")     == false
  isNotBlank("\r\n") == false
  isNotBlank("\t")   == false
  isNotBlank("   ")  == false
  isNotBlank("a")    == true
  isNotBlank(".")    == true
  isNotBlank(";\n")  == true
Parameters
str:String

the string to test.

Returns:boolean

true if string is not null and has at least 1 non-whitespace character, false if null or all-whitespace characters.

nonNullback to summary
public static String nonNull(String s)

Return a non null string.

Parameters
s:String

String

Returns:String

The string passed in or empty string if it is null.

printableback to summary
public static String printable(String name)
printableback to summary
public static String printable(byte[] b)
randomAlphaNumericback to summary
public static String randomAlphaNumeric(int digits)
replaceback to summary
public static String replace(String str, char find, char with)

Replace chars within string.

Fast replacement for java.lang.String#String#replace(char, char)

Parameters
str:String

the input string

find:char

the char to look for

with:char

the char to replace with

Returns:String

the now replaced string

replaceback to summary
public static String replace(String s, String sub, String with)

Replace substrings within string.

Fast replacement for java.lang.String#String#replace(CharSequence, CharSequence)

Parameters
s:String

the input string

sub:String

the string to look for

with:String

the string to replace with

Returns:String

the now replaced string

replaceFirstback to summary
public static String replaceFirst(String original, String target, String replacement)

Replace first substrings within string.

Fast replacement for java.lang.String#String#replaceFirst(String, String), but without Regex support.

Parameters
original:String

the original string

target:String

the target string to look for

replacement:String

the replacement string to use

Returns:String

the replaced string

sanitizeFileSystemNameback to summary
public static String sanitizeFileSystemName(String str)

Replace all characters from input string that are known to have special meaning in various filesystems.

This will replace all of the following characters with a "_" (underscore).

  • Control Characters
  • Anything not 7-bit printable ASCII
  • Special characters: pipe, redirect, combine, slash, equivalence, bang, glob, selection, etc...
  • Space
Parameters
str:String

the raw input string

Returns:String

the sanitized output string. or null if str is null.

sanitizeXmlStringback to summary
public static String sanitizeXmlString(String html)
startsWithIgnoreCaseback to summary
public static boolean startsWithIgnoreCase(String string, String prefix)

Check for a string prefix, ignoring StandardCharsets#US_ASCII case differences.

Parameters
string:String

The string to check

prefix:String

The sub string to look for as a prefix

Returns:boolean

true if the string ends with the substring, ignoring StandardCharsets#US_ASCII case differences.

Annotations
@Deprecated
stripback to summary
public static String strip(String str, String find)
toHexStringback to summary
public static String toHexString(byte b)
toHexStringback to summary
public static String toHexString(byte[] b)
toHexStringback to summary
public static String toHexString(byte[] b, int offset, int length)
toIntback to summary
public static int toInt(String string, int from)

Convert String to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown

Parameters
string:String

A String containing an integer.

from:int

The index to start parsing from

Returns:int

an int

toStringback to summary
public static String toString(byte[] b, int offset, int length, String charset)

Deprecated

for removal since 10.

use String#String(byte[], int, int, Charset) instead

Annotations
@Deprecated
since:10
forRemoval:true
toUTF8Stringback to summary
public static String toUTF8String(byte[] b, int offset, int length)
truncateback to summary
public static String truncate(String str, int maxSize)

Truncate a string to a max size.

Parameters
str:String

the string to possibly truncate

maxSize:int

the maximum size of the string

Returns:String

the truncated string. if str param is null, then the returned string will also be null.

valueOfback to summary
public static String valueOf(Object object)

The String value of an Object

This method calls String#valueOf(Object) unless the object is null, in which case null is returned

Parameters
object:Object

The object

Returns:String

String value or null

org.eclipse.jetty.util back to summary

pack-priv final Enum StringUtil.CsvSplitState

extends Enum<StringUtil.CsvSplitState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final StringUtil.CsvSplitState
public static final StringUtil.CsvSplitState
public static final StringUtil.CsvSplitState
public static final StringUtil.CsvSplitState
public static final StringUtil.CsvSplitState
public static final StringUtil.CsvSplitState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static StringUtil.CsvSplitState
public static StringUtil.CsvSplitState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DATAback to summary
public static final StringUtil.CsvSplitState DATA
POST_DATAback to summary
public static final StringUtil.CsvSplitState POST_DATA
PRE_DATAback to summary
public static final StringUtil.CsvSplitState PRE_DATA
QUOTEback to summary
public static final StringUtil.CsvSplitState QUOTE
SLOSHback to summary
public static final StringUtil.CsvSplitState SLOSH
WHITEback to summary
public static final StringUtil.CsvSplitState WHITE

Constructor Detail

CsvSplitStateback to summary
private CsvSplitState()

Method Detail

valueOfback to summary
public static StringUtil.CsvSplitState valueOf(String name)
valuesback to summary
public static StringUtil.CsvSplitState[] values()