String Type

String - string type, encapsulated with "" when used, any character (Chinese, English, symbols, etc.) can be written in the middle

Define String: String string = ""

example:

String string = "Zhang San"

Methods of String type:

  • string.contains(<String str>): Determine whether a string contains a specific sequence or character

  Return value type: Boolean

  example:

"fx is great".contains("fx") //return: true

  • string.startsWith(<String str>): Check if it starts with a specific string

  Return value type: Boolean

  example:

"fx is great".startsWith("fx") //return: true

  • string.endsWith(<String str>): Check if it ends with a specific string

  Return value type: Boolean

  example:

"fx is great".endWith("fx") //return: false

  • string.concat(<String str>): Add the specified string to the end of this string

  Return value type: String

  example:

"fx is great".concat("fx") //return: fx is greatfx

  • string.isEmpty(): Determine whether it is empty

  Return value type: Boolean

  example:

"fx ".isEmpty() //return: false

  • string.trim(): returns a copy of the string, ignoring leading and trailing blanks

  Return value type: String

  example:

" Welcome to fxiaoke Creator ".trim() //Return: Welcome to fxiaoke Creator

  • string.replace(<String searchString>,<String replacement>): Use the given parameter replacement to replace all substrings of the string that match the given searchString

  Return value type: String

  example:

"Red,Green,Green".replace("Green","Blue") //Return: Red,Blue,Blue

  • string.replaceAll(<String regex>,<String replacement>): Use the given parameter replacement to replace all substrings of the string that match the given regex (regular expression)

  Return value type: String

  example:

"Red,Green,Green".replaceAll("Green","Blue") //Return: Red,Blue,Blue

  • string.substring(<Integer startIndex>,<Integer endIndex>): returns a new string which is a substring in this string

  Return value type: String

  example:

"www.fxiaoke.com".substring(4,11) //Return: fxiaoke

Note: In the programming language, it starts from the 0th digit, so the fourth digit in the above example is f; the final returned result contains startIndex, not endIndex

  • string.split(<String regex>) or string.split(<String regex>,<Integer limit>) splits the string based on matching the given regular expression

  Return value type: List

  example:

"www-fxiaoke-com".split("-") //Return: ["www","fxiaoke","com"]

"www-fxiaoke-com".split("-","2") //Return: ["www","fxiaoke-com"]

Note: In string.split(<String regex>,<Integer limit>), limit means splitting the string into several strings at most

  • string.length() returns the length of this string

  Return value type: BigDecimal

  example:

"www.fxiaoke.com".length() //Return: 15

  • string.indexOf(<String subString>) returns the index of the first occurrence of the specified substring in this string, starting from the specified index (starting from 0 by default)

Or string.indexOf(<String subString>,<SBigDecimal fromIndex>) returns the index of the first occurrence of the specified substring after fromIndex in this string, starting from the specified index (starting from 0 by default)

  Return value type: BigDecimal

  example:
> "www.fxiaoke.com".indexOf("o") //Return: 8

> "www.fxiaoke.com".indexOf("o", 9) //return: 13
  • string.lastIndexOf(<String subString>) returns the index of the last occurrence of the specified substring in this string, starting the reverse search from the specified index

Or string.lastIndexOf(<String subString>,<BigDecimal fromIndex>) returns the index of the last occurrence of the specified substring in this string before lastIndexOf, and starts reverse search from the specified index

  Return value type: BigDecimal

  example:
> "www.fxiaoke.com".indexOf("o") //Return: 13

> "www.fxiaoke.com".indexOf("o",9) //Return: 8
  • string.toUpperCase(): Convert to uppercase letters

  Return value type: String

  example:

"abc".toUpperCase() //return: ABC

  • string.toLowerCase(): Convert to lowercase letters

  Return value type: String

  example:

"ABC".toLowerCase() //return: abc

2024-07-12
0 0