Strings

object Strings

Operations on String that are <code>null</code> safe.

Since

3.0

class Object
trait Matchable
class Any

Value members

Concrete methods

def abbreviate(str: String, maxWidth: Int): String
def abbreviate(str: String, offset: Int, maxWidth: Int): String
def capitalize(str: String): String

Capitalizes a String changing the first letter to title case as per [[Character#toTitleCase(char)]]. No other letters are changed. For a word based algorithm, see returns <code>null</code>.

Capitalizes a String changing the first letter to title case as per [[Character#toTitleCase(char)]]. No other letters are changed. For a word based algorithm, see returns <code>null</code>.

capitalize(null)  = null
capitalize("")    = ""
capitalize("cat") = "Cat"
capitalize("cAt") = "CAt"
Value Params
str

the String to capitalize, may be null

Returns

the capitalized String, { @code null} if null String input

See also

#uncapitalize(String)

Since

2.0

def concat(seq: Any*): String

concat.

concat.

Value Params
seq

a String object.

Returns

a String object.

def contains(seq: CharSequence, searchSeq: CharSequence): Boolean

Checks if CharSequence contains a search CharSequence, handling <code>null</code>. This method uses [[String#indexOf(String)]] if possible. A <code>null</code> CharSequence will return <code>false</code>.

Checks if CharSequence contains a search CharSequence, handling <code>null</code>. This method uses [[String#indexOf(String)]] if possible. A <code>null</code> CharSequence will return <code>false</code>.

contains(null, *)     = false
contains(*, null)     = false
contains("", "")      = true
contains("abc", "")   = true
contains("abc", "a")  = true
contains("abc", "z")  = false
Value Params
searchSeq

the CharSequence to find, may be null

seq

the CharSequence to check, may be null

Returns

true if the CharSequence contains the search CharSequence, false if not or { @code null} string input

def contains(seq: CharSequence, searchChar: Int): Boolean

Checks if CharSequence contains a search character, handling <code>null</code>. This method uses [[String#indexOf(int)]] if possible. A <code>null</code> or empty ("") CharSequence will return <code>false</code>.

Checks if CharSequence contains a search character, handling <code>null</code>. This method uses [[String#indexOf(int)]] if possible. A <code>null</code> or empty ("") CharSequence will return <code>false</code>.

contains(null, *)    = false
contains("", *)      = false
contains("abc", 'a') = true
contains("abc", 'z') = false
Value Params
searchChar

the character to find

seq

the CharSequence to check, may be null

Returns

true if the CharSequence contains the search character, false if not or { @code null} string input

Since

2.0

def count(host: String, charactor: Char): Int

count char in host string

count char in host string

Value Params
charactor

a char.

host

String object.

Returns

a int.

def count(host: String, searchStr: String): Int

count inner string in host string

count inner string in host string

Value Params
host

a String object.

searchStr

a String object.

Returns

a int.

def defaultIfBlank[T <: CharSequence](str: T, defaultStr: T): T

Returns either the passed in CharSequence, or if the CharSequence is whitespace, empty ("") or <code>null</code>, the value of <code>defaultStr</code>.

Returns either the passed in CharSequence, or if the CharSequence is whitespace, empty ("") or <code>null</code>, the value of <code>defaultStr</code>.

def format(format: String, args: Any*): String
def insert(str: String, c: String, pos: Int): String

insert.

insert.

Value Params
c

a String object.

pos

a int.

str

a String object.

Returns

a String object.

def insert(str: String, contnets: String, beginAt: Int, endAt: Int): String

replace [bigen,end] [1...end] with givenStr 可以使用StringBuilder的replace方法替换该方法

replace [bigen,end] [1...end] with givenStr 可以使用StringBuilder的replace方法替换该方法

Value Params
beginAt

a int.

contnets

a String object.

endAt

a int.

str

a String object.

Returns

a String object.

def intersectSeq(first: String, second: String): String

intersectSeq.

intersectSeq.

Value Params
first

a String object.

second

a String object.

Returns

a String object.

def intersectSeq(first: String, second: String, delimiter: String): String

返回一个新的逗号相隔字符串,实现其中的单词a-b的功能

返回一个新的逗号相隔字符串,实现其中的单词a-b的功能

Value Params
delimiter

a String object.

first

a String object.

second

a String object.

Returns

a String object.

def isBlank(cs: CharSequence): Boolean

Checks if a CharSequence is whitespace, empty ("") or null.

Checks if a CharSequence is whitespace, empty ("") or null.

isBlank(null)      = true
isBlank("")        = true
isBlank(" ")       = true
isBlank("bob")     = false
isBlank("  bob  ") = false
Value Params
cs

the CharSequence to check, may be null

Returns

{ @code true} if the CharSequence is null, empty or whitespace

Since

3.0

@inline
def isEmpty(cs: CharSequence): Boolean

Returns true is cs is null or cs.length equals 0.

Returns true is cs is null or cs.length equals 0.

def isEqualSeq(first: String, second: String): Boolean

isEqualSeq.

isEqualSeq.

Value Params
first

not null

second

not null

Returns

a boolean.

def isEqualSeq(first: String, second: String, delimiter: String): Boolean

判断两个","逗号相隔的字符串中的单词是否完全等同.

判断两个","逗号相隔的字符串中的单词是否完全等同.

Value Params
delimiter

a String object.

first

a String object.

second

a String object.

Returns

a boolean.

def isNotBlank(cs: CharSequence): Boolean

Checks if a CharSequence is not empty (""), not null and not whitespace only.

Checks if a CharSequence is not empty (""), not null and not whitespace only.

isNotBlank(null)      = false
isNotBlank("")        = false
isNotBlank(" ")       = false
isNotBlank("bob")     = true
isNotBlank("  bob  ") = true
Value Params
cs

the CharSequence to check, may be null

Returns

{ @code true} if the CharSequence is not empty and not null and not whitespace

Since

3.0

@inline
def isNotEmpty(cs: CharSequence): Boolean

Return true if cs not null and cs has length.

Return true if cs not null and cs has length.

def join(seq: Iterable[_], delimiter: String): String

join.

join.

Value Params
delimiter

a String object.

Returns

a String object.

def join(seq: String*): String

join.

join.

Value Params
seq

a String object.

Returns

a String object.

def join(seq: Array[String], delimiter: String): String

将数组中的字符串,用delimiter串接起来.<br> 首尾不加delimiter

将数组中的字符串,用delimiter串接起来.<br> 首尾不加delimiter

Value Params
delimiter

a String object.

seq

an array of String objects.

Returns

a String object.

def keepSeqUnique(keyString: String): String

保持逗号分隔的各个单词都是唯一的。并且按照原来的顺序存放。

保持逗号分隔的各个单词都是唯一的。并且按照原来的顺序存放。

Value Params
keyString

a String object.

Returns

a String object.

def leftPad(str: String, size: Int, padChar: Char): String

Left pad a String with a specified character. Pad to a size of <code>size</code>.

Left pad a String with a specified character. Pad to a size of <code>size</code>.

leftPad(null, *, *)     = null
leftPad("", 3, 'z')     = "zzz"
leftPad("bat", 3, 'z')  = "bat"
leftPad("bat", 5, 'z')  = "zzbat"
leftPad("bat", 1, 'z')  = "bat"
leftPad("bat", -1, 'z') = "bat"
Value Params
padChar

the character to pad with

size

the size to pad to

str

the String to pad out, may be null

Returns

left padded String or original String if no padding is necessary, { @code null} if null String input

Since

3.0

def lowerCase(s: String): String
def mergeSeq(first: String, second: String): String

mergeSeq.

mergeSeq.

Value Params
first

a String object.

second

a String object.

Returns

a String object.

def mergeSeq(first: String, second: String, delimiter: String): String

将两个用delimiter串起来的字符串,合并成新的串,重复的"单词"只出现一次. 如果第一个字符串以delimiter开头,第二个字符串以delimiter结尾,<br> 合并后的字符串仍以delimiter开头和结尾.<br>

将两个用delimiter串起来的字符串,合并成新的串,重复的"单词"只出现一次. 如果第一个字符串以delimiter开头,第二个字符串以delimiter结尾,<br> 合并后的字符串仍以delimiter开头和结尾.<br>

<blockquote>

mergeSeq(&quot;,1,2,&quot;, &quot;&quot;) = &quot;,1,2,&quot;;
mergeSeq(&quot;,1,2,&quot;, null) = &quot;,1,2,&quot;;
mergeSeq(&quot;1,2&quot;, &quot;3&quot;) = &quot;1,2,3&quot;;
mergeSeq(&quot;1,2&quot;, &quot;3,&quot;) = &quot;1,2,3,&quot;;
mergeSeq(&quot;,1,2&quot;, &quot;3,&quot;) = &quot;,1,2,3,&quot;;
mergeSeq(&quot;,1,2,&quot;, &quot;,3,&quot;) = &quot;,1,2,3,&quot;;

</blockquote>

Value Params
delimiter

a String object.

first

a String object.

second

a String object.

Returns

a String object.

def remove(str: String, remove: Char): String

Removes all occurrences of a character from within the source string. A <code>null</code> source string will return <code>null</code>. An empty ("") source string will return the empty string.

Removes all occurrences of a character from within the source string. A <code>null</code> source string will return <code>null</code>. An empty ("") source string will return the empty string.

StringUtils.remove(null, *) = null
StringUtils.remove("", *) = ""
StringUtils.remove("queued", 'u') = "qeed"
StringUtils.remove("queued", 'z') = "queued"
Value Params
remove

the char to search for and remove, may be null

str

the source String to search, may be null

Returns

the substring with the char removed if found, { @code null} if null String input

def removeWord(host: String, word: String): String

removeWord.

removeWord.

Value Params
host

a String object.

word

a String object.

Returns

a String object.

def removeWord(host: String, word: String, delimiter: String): String

removeWord.

removeWord.

Value Params
delimiter

a String object.

host

a String object.

word

a String object.

Returns

a String object.

def repeat(ch: Char, repeat: Int): String

Returns padding using the specified delimiter repeated to a given length.

Returns padding using the specified delimiter repeated to a given length.

repeat(0, 'e')  = ""
repeat(3, 'e')  = "eee"
repeat(-2, 'e') = ""
Value Params
ch

character to repeat

repeat

number of times to repeat char, negative treated as zero

Returns

String with repeated character

See also

#repeat(String, int)

def repeat(str: String, repeat: Int): String

Repeat a String <code>repeat</code> times to form a new String.

Repeat a String <code>repeat</code> times to form a new String.

repeat(null, 2) = null
repeat("", 0)   = ""
repeat("", 2)   = ""
repeat("a", 3)  = "aaa"
repeat("ab", 2) = "abab"
repeat("a", -2) = ""
Value Params
repeat

number of times to repeat str, negative treated as zero

str

the String to repeat, may be null

Returns

a new String consisting of the original String repeated, { @code null} if null String input

Since

3.0

def replace(text: String, searchString: String, replacement: String): String

Replaces all occurrences of a String within another String. A <code>null</code> reference passed to this method is a no-op.

Replaces all occurrences of a String within another String. A <code>null</code> reference passed to this method is a no-op.

replace(null, *, *)        = null
replace("", *, *)          = ""
replace("any", null, *)    = "any"
replace("any", *, null)    = "any"
replace("any", "", *)      = "any"
replace("aba", "a", null)  = "aba"
replace("aba", "a", "")    = "b"
replace("aba", "a", "z")   = "zbz"
Value Params
replacement

the String to replace it with, may be null

searchString

the String to search for, may be null

text

text to search and replace in, may be null

Returns

the text with any replacements processed, { @code null} if null String input

def rightPad(str: String, size: Int, padChar: Char): String

Right pad a String with a specified character. The String is padded to the size of <code>size</code>.

Right pad a String with a specified character. The String is padded to the size of <code>size</code>.

rightPad(null, *, *)     = null
rightPad("", 3, 'z')     = "zzz"
rightPad("bat", 3, 'z')  = "bat"
rightPad("bat", 5, 'z')  = "batzz"
rightPad("bat", 1, 'z')  = "bat"
rightPad("bat", -1, 'z') = "bat"
Value Params
padChar

the character to pad with

size

the size to pad to

str

the String to pad out, may be null

Returns

right padded String or original String if no padding is necessary, { @code null} if null String input

Since

3.0

def split(target: String): Array[String]

split.

split.

Value Params
target

a String object.

Returns

an array of String objects.

def split(str: String, separatorChar: Char): Array[String]

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer. A <code>null</code> input String returns <code>null</code>.

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer. A <code>null</code> input String returns <code>null</code>.

split(null, *)         = null
split("", *)           = []
split("a.b.c", '.')    = ["a", "b", "c"]
split("a..b.c", '.')   = ["a", "b", "c"]
split("a:b:c", '.')    = ["a:b:c"]
split("a b c", ' ')    = ["a", "b", "c"]
def split(target: String, separatorChars: Array[Char]): Array[String]

split with separators

split with separators

Value Params
separatorChars

an array of char.

target

a String object.

Returns

an array of String objects.

def split(str: String, separatorChars: String): Array[String]

Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer. A <code>null</code> input String returns <code>null</code>. A <code>null</code> separatorChars splits on whitespace.

Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer. A <code>null</code> input String returns <code>null</code>. A <code>null</code> separatorChars splits on whitespace.

split(null, *)         = null
split("", *)           = []
split("abc def", null) = ["abc", "def"]
split("abc def", " ")  = ["abc", "def"]
split("abc  def", " ") = ["abc", "def"]
split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
def splitNumSeq(numSeq: String): Array[Int]

将1-2,3,4-9之类的序列拆分成数组

将1-2,3,4-9之类的序列拆分成数组

Value Params
numSeq

a String object.

Returns

an array of Int objects.

def splitToInt(ids: String): Seq[Int]

splitToInteger.

splitToInteger.

def splitToLong(ids: String): Seq[Long]

splitToLong.

splitToLong.

def stripEnd(str: String, stripChars: String): String

Strips any of a set of characters from the end of a String.

Strips any of a set of characters from the end of a String.

A <code>null</code> input String returns <code>null</code>. An empty string ("") input returns the empty string.

If the stripChars String is <code>null</code>, whitespace is stripped as defined by [[Character#isWhitespace(char)]].

stripEnd(null, *)          = null
stripEnd("", *)            = ""
stripEnd("abc", "")        = "abc"
stripEnd(*, null)    = *
stripEnd("  abcyx", "xyz") = "  abc"
Value Params
str

the String to remove characters from, may be null

stripChars

the characters to remove, null treated as whitespace

Returns

the stripped String, <code>null</code> if null String input

def substring(str: String, startIndex: Int, endIndex: Int): String

Gets a substring from the specified String avoiding exceptions.

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start/end <code>n</code> characters from the end of the String. The returned substring starts with the character in the <code>start</code> position and ends before the <code>end</code> position. All position counting is zero-based -- i.e., to start at the beginning of the string use <code>start = 0</code>. Negative start and end positions can be used to specify offsets relative to the end of the String. If <code>start</code> is not strictly to the left of <code>end</code>, "" is returned.

substring(null, *, *)    = null
substring("", * ,  *)    = "";
substring("abc", 0, 2)   = "ab"
substring("abc", 2, 0)   = ""
substring("abc", 2, 4)   = "c"
substring("abc", 4, 6)   = ""
substring("abc", 2, 2)   = ""
substring("abc", -2, -1) = "b"
substring("abc", -4, 2)  = "ab"
Value Params
endIndex

the position to end at (exclusive), negative means count back from the end of the String by this many characters

startIndex

the position to start from, negative means count back from the end of the String by this many characters

str

the String to get the substring from, may be null

Returns

substring from start position to end position, { @code null} if null String input

def substringAfter(str: String, separator: String): String

Gets the substring after the first occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. A <code>null</code> separator will return the empty string if the input string is not <code>null</code>. If nothing is found, the empty string is returned.

Gets the substring after the first occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. A <code>null</code> separator will return the empty string if the input string is not <code>null</code>. If nothing is found, the empty string is returned.

substringAfter(null, *)      = null
substringAfter("", *)        = ""
substringAfter(*, null)      = ""
substringAfter("abc", "a")   = "bc"
substringAfter("abcba", "b") = "cba"
substringAfter("abc", "c")   = ""
substringAfter("abc", "d")   = ""
substringAfter("abc", "")    = "abc"
Value Params
separator

the String to search for, may be null

str

the String to get a substring from, may be null

Returns

the substring after the first occurrence of the separator, { @code null} if null String input

Since

2.0

def substringAfterLast(str: String, separator: String): String

Gets the substring after the last occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. An empty or <code>null</code> separator will return the empty string if the input string is not <code>null</code>. If nothing is found, the empty string is returned.

Gets the substring after the last occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. An empty or <code>null</code> separator will return the empty string if the input string is not <code>null</code>. If nothing is found, the empty string is returned.

substringAfterLast(null, *)      = null
substringAfterLast("", *)        = ""
substringAfterLast(*, "")        = ""
substringAfterLast(*, null)      = ""
substringAfterLast("abc", "a")   = "bc"
substringAfterLast("abcba", "b") = "a"
substringAfterLast("abc", "c")   = ""
substringAfterLast("a", "a")     = ""
substringAfterLast("a", "z")     = ""
Value Params
separator

the String to search for, may be null

str

the String to get a substring from, may be null

Returns

the substring after the last occurrence of the separator, { @code null} if null String input

Since

3.0

def substringBefore(str: String, separator: String): String

Gets the substring before the first occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. A <code>null</code> separator will return the input string. If nothing is found, the string input is returned.

Gets the substring before the first occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. A <code>null</code> separator will return the input string. If nothing is found, the string input is returned.

substringBefore(null, *)      = null
substringBefore("", *)        = ""
substringBefore("abc", "a")   = ""
substringBefore("abcba", "b") = "a"
substringBefore("abc", "c")   = "ab"
substringBefore("abc", "d")   = "abc"
substringBefore("abc", "")    = ""
substringBefore("abc", null)  = "abc"
Value Params
separator

the String to search for, may be null

str

the String to get a substring from, may be null

Returns

the substring before the first occurrence of the separator, { @code null} if null String input

Since

2.0

def substringBeforeLast(str: String, separator: String): String

Gets the substring before the last occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. An empty or <code>null</code> separator will return the input string. If nothing is found, the string input is returned.

Gets the substring before the last occurrence of a separator. The separator is not returned. A <code>null</code> string input will return <code>null</code>. An empty ("") string input will return the empty string. An empty or <code>null</code> separator will return the input string. If nothing is found, the string input is returned.

substringBeforeLast(null, *)      = null
substringBeforeLast("", *)        = ""
substringBeforeLast("abcba", "b") = "abc"
substringBeforeLast("abc", "c")   = "ab"
substringBeforeLast("a", "a")     = ""
substringBeforeLast("a", "z")     = "a"
substringBeforeLast("a", null)    = "a"
substringBeforeLast("a", "")      = "a"
Value Params
separator

the String to search for, may be null

str

the String to get a substring from, may be null

Returns

the substring before the last occurrence of the separator, { @code null} if null String input

Since

3.0

def substringBetween(str: String, open: String, close: String): String

Gets the String that is nested in between two Strings. Only the first match is returned. A <code>null</code> input String returns <code>null</code>. A <code>null</code> open/close returns <code>null</code> (no match). An empty ("") open and close returns an empty string.

Gets the String that is nested in between two Strings. Only the first match is returned. A <code>null</code> input String returns <code>null</code>. A <code>null</code> open/close returns <code>null</code> (no match). An empty ("") open and close returns an empty string.

substringBetween("wx[b]yz", "[", "]") = "b"
substringBetween(null, *, *)          = null
substringBetween(*, null, *)          = null
substringBetween(*, *, null)          = null
substringBetween("", "", "")          = ""
substringBetween("", "", "]")         = null
substringBetween("", "[", "]")        = null
substringBetween("yabcz", "", "")     = ""
substringBetween("yabcz", "y", "z")   = "abc"
substringBetween("yabczyabcz", "y", "z")   = "abc"
Value Params
close

the String after the substring, may be null

open

the String before the substring, may be null

str

the String containing the substring, may be null

Returns

the substring, { @code null} if no match

Since

3.0

def subtractSeq(first: String, second: String): String

subtractSeq.

subtractSeq.

Value Params
first

a String object.

second

a String object.

Returns

a String object.

def subtractSeq(first: String, second: String, delimiter: String): String

返回一个新的逗号相隔字符串,实现其中的单词a-b的功能. 新的字符串将以,开始,结束<br>

返回一个新的逗号相隔字符串,实现其中的单词a-b的功能. 新的字符串将以,开始,结束<br>

def trim(str: String): String

Removes control characters (char <= 32) from both ends of this String, handling <code>null</code> by returning <code>null</code>. The String is trimmed using [[String#trim()]]. Trim removes start and end characters <= 32.

Removes control characters (char <= 32) from both ends of this String, handling <code>null</code> by returning <code>null</code>. The String is trimmed using [[String#trim()]]. Trim removes start and end characters <= 32.

trim(null)          = null
trim("")            = ""
trim("     ")       = ""
trim("abc")         = "abc"
trim("    abc    ") = "abc"
Value Params
str

the String to be trimmed, may be null

Returns

the trimmed string, { @code null} if null String input

Since

3.0

def trimEnd(str: String): String
def unCamel(str: String): String

unCamel.

unCamel.

def unCamel(str: String, seperator: Char): String

unCamel.

unCamel.

Value Params
seperator

a char.

str

a String object.

Returns

a String object.

def unCamel(str: String, seperator: Char, lowercase: Boolean): String

将驼峰表示法转换为下划线小写表示

将驼峰表示法转换为下划线小写表示

Value Params
lowercase

a boolean.

seperator

a char.

str

a String object.

Returns

a String object.

def uncapitalize(str: String): String

Uncapitalizes a String changing the first letter to title case as per java.lang.Character#toLowerCase. No other letters are changed. For a word based algorithm, see String returns <code>null</code>.

Uncapitalizes a String changing the first letter to title case as per java.lang.Character#toLowerCase. No other letters are changed. For a word based algorithm, see String returns <code>null</code>.

uncapitalize(null)  = null
uncapitalize("")    = ""
uncapitalize("Cat") = "cat"
uncapitalize("CAT") = "cAT"
Value Params
str

the String to uncapitalize, may be null

Returns

the uncapitalized String, { @code null} if null String input

See also

#capitalize(String)

Since

3.0

def upperCase(s: String): String

Concrete fields

val DELIMITER: String

Constant <code>DELIMITER=","</code>

Constant <code>DELIMITER=","</code>