English
String
About 471 wordsAbout 2 min
2026-01-09
String - A sequence of characters enclosed in double quotes ("") that can contain any characters (Chinese, English, symbols, etc.)
- Define a String:
String string = ""
Example:
String string = "Zhang San"- String methods:
string.contains(<String str>): Checks if the string contains a specific sequence or character
Return type: Boolean
Example:
"fx is great".contains("fx") // Returns: truestring.startsWith(<String str>): Checks if the string starts with the specified prefix
Return type: Boolean
Example:
"fx is great".startsWith("fx") // Returns: truestring.endsWith(<String str>): Checks if the string ends with the specified suffix
Return type: Boolean
Example:
"fx is great".endsWith("fx") // Returns: falsestring.concat(<String str>): Concatenates the specified string to the end of this string
Return type: String
Example:
"fx is great".concat("fx") // Returns: fx is greatfxstring.isEmpty(): Checks if the string is empty
Return type: Boolean
Example:
"fx ".isEmpty() // Returns: falsestring.trim(): Returns a copy of the string with leading and trailing whitespace omitted
Return type: String
Example:
" Welcome to fxiaoke Creator ".trim() // Returns: Welcome to fxiaoke Creatorstring.replace(<String searchString>,<String replacement>): Replaces all occurrences of searchString with replacement
Return type: String
Example:
"Red,Green,Green".replace("Green","Blue") // Returns: Red,Blue,Bluestring.replaceAll(<String regex>,<String replacement>): Replaces all substrings matching the regex with replacement
Return type: String
Example:
"Red,Green,Green".replaceAll("Green","Blue") // Returns: Red,Blue,Bluestring.substring(<Integer startIndex>,<Integer endIndex>): Returns a new substring from startIndex (inclusive) to endIndex (exclusive)
Return type: String
Example:
"www.fxiaoke.com".substring(4,11) // Returns: fxiaokeNote: Index starts at 0, so position 4 is 'f'. The result includes startIndex but excludes endIndex.
string.split(<String regex>) or string.split(<String regex>,<Integer limit>): Splits the string around matches of the given regex
Return type: String[]
Example:
"www-fxiaoke-com".split("-") // Returns: ["www","fxiaoke","com"]
"www-fxiaoke-com".split("-","2") // Returns: ["www","fxiaoke-com"]Note: The limit parameter controls the maximum number of splits.
string.length(): Returns the length of the string
Return type: BigDecimal
Example:
"www.fxiaoke.com".length() // Returns: 15string.indexOf(<String subString>): Returns the index of the first occurrence of the substring (starting from 0 by default)
or string.indexOf(<String subString>,<BigDecimal fromIndex>): Returns the index of the first occurrence after fromIndex
Return type: BigDecimal
Example:
"www.fxiaoke.com".indexOf("o") // Returns: 8
"www.fxiaoke.com".indexOf("o",9) // Returns: 13string.lastIndexOf(<String subString>): Returns the index of the last occurrence of the substring
or string.lastIndexOf(<String subString>,<BigDecimal fromIndex>): Returns the index of the last occurrence before fromIndex
Return type: BigDecimal
Example:
"www.fxiaoke.com".lastIndexOf("o") // Returns: 13
"www.fxiaoke.com".lastIndexOf("o",9) // Returns: 8string.toUpperCase(): Converts all characters to uppercase
Return type: String
Example:
"abc".toUpperCase() // Returns: ABCstring.toLowerCase(): Converts all characters to lowercase
Return type: String
Example:
"ABC".toLowerCase() // Returns: abc