String Class
Contains methods for the String primitive data type.
All string method definitions adhere to the Unicode Standard. For example, Unicode Roman numerals are classified as a type of number form, not a type of digit. Therefore, string methods such as isAlphanumeric() return false if used on a String that contains a Roman numeral. For Unicode classifications, see the Unicode Character Code Charts.
For more information on Strings, see String Data Type.
The following are methods for String.
-
Returns an abbreviated version of the String, of the specified length and with ellipses appended if the current String is longer than the specified length; otherwise, returns the original String without ellipses.
-
Returns an abbreviated version of the String, starting at the specified character offset and of the specified length. The returned String has ellipses appended at the start and the end if characters have been removed at these locations.
-
Returns the current String with the first letter changed to title case.
-
Returns a version of the current String of the specified size padded with spaces on the left and right, so that it appears in the center. If the specified size is smaller than the current String size, the entire String is returned without added spaces.
-
Returns a version of the current String of the specified size padded with the specified String on the left and right, so that it appears in the center. If the specified size is smaller than the current String size, the entire String is returned without padding.
-
Returns the value of the character at the specified index.
-
Returns the Unicode code point value at the specified index.
-
Returns the Unicode code point value that occurs before the specified index.
-
codePointCount(beginIndex, endIndex)
Returns the number of Unicode code points within the specified text range.
-
Compares two strings lexicographically, based on the Unicode value of each character in the Strings.
-
Returns
trueif and only if the String that called the method contains the specified sequence of characters in substring. -
Returns
trueif the current String contains any of the characters in the specified String; otherwise, returnsfalse. -
Returns
trueif the current String contains the specified sequence of characters without regard to case; otherwise, returnsfalse. -
Returns
trueif the current String doesn’t contain any of the characters in the specified String; otherwise, returnsfalse. -
Returns
trueif the current String contains characters only from the specified sequence of characters and not any other characters; otherwise, returnsfalse. -
Returns
trueif the current String contains any white space characters; otherwise, returnsfalse. -
Returns the number of times the specified substring occurs in the current String.
-
Returns a version of the current String with all white space characters removed.
-
Returns the difference between the current String and the specified String.
-
Returns
trueif the String that called the method ends with the specified suffix. -
Returns
trueif the current String ends with the specified suffix; otherwise, returnsfalse. -
Deprecated. This method is replaced by
equals(stringOrId). Returnstrueif the passed-in string is not null and represents the same binary sequence of characters as the current string. Use this method to perform case-sensitive comparisons. -
Returns
trueif the passed-in object is not null and represents the same binary sequence of characters as the current string. Use this method to compare a string to an object that represents a string or an ID. -
equalsIgnoreCase(secondString)
Returns
trueif the secondString isn’t null and represents the same sequence of characters as the String that called the method, ignoring case. -
Returns a String for a CSV column enclosed in double quotes, if required.
-
Escapes the characters in the String using EcmaScript String rules.
-
Escapes the characters in a String using HTML 3.0 entities.
-
Escapes the characters in a String using HTML 4.0 entities.
-
Returns a String whose characters are escaped using Java String rules. Characters escaped include quotes and control characters, such as tab, backslash, and carriage return characters.
-
escapeSingleQuotes(stringToEscape)
Returns a String with the escape character (
\) added before any single quotation mark (') or backslash (\) in the String s. -
Returns a String whose Unicode characters are escaped to a Unicode escape sequence.
-
Escapes the characters in a String using XML entities.
-
format(stringToFormat, formattingArguments)
Treat the first argument as a pattern and return a string using the second argument for substitution and formatting. The substitution and formatting are the same as
apex:outputTextand the JavaMessageFormatclass. Non-string types in the second argument’s List are implicitly converted to strings, respecting the toString() method overrides that exist on the type. -
Returns a String from the values of the list of integers.
-
Returns an array of character values that represent the characters in this string.
-
Returns the initial sequence of characters as a String that is common to all the specified Strings.
-
getLevenshteinDistance(stringToCompare)
Returns the Levenshtein distance between the current String and the specified String.
-
getLevenshteinDistance(stringToCompare, threshold)
Returns the Levenshtein distance between the current String and the specified String if it is less than or equal than the given threshold; otherwise, returns -1.
-
Returns a hash code value for this string.
-
Returns the index of the first occurrence of the specified substring. If the substring does not occur, this method returns -1.
-
Returns the zero-based index of the first occurrence of the specified substring from the point of the given index. If the substring does not occur, this method returns -1.
-
Returns the zero-based index of the first occurrence of any character specified in the substring. If none of the characters occur, returns -1.
-
Returns the zero-based index of the first occurrence of a character that is not in the specified substring. Otherwise, returns -1.
-
Returns the index of the first occurrence of the character that corresponds to the specified character value.
-
indexOfChar(character, startIndex)
Returns the index of the first occurrence of the character that corresponds to the specified character value, starting from the specified index.
-
indexOfDifference(stringToCompare)
Returns the zero-based index of the character where the current String begins to differ from the specified String.
-
Returns the zero-based index of the first occurrence of the specified substring without regard to case. If the substring does not occur, this method returns -1.
-
indexOfIgnoreCase(substring, startPosition)
Returns the zero-based index of the first occurrence of the specified substring from the point of index i, without regard to case. If the substring does not occur, this method returns -1.
-
Returns
trueif all characters in the current String are lowercase; otherwise, returnsfalse. -
Returns
trueif all characters in the current String are uppercase; otherwise, returnsfalse. -
Returns
trueif all characters in the current String are Unicode letters only; otherwise, returnsfalse. -
Returns
trueif all characters in the current String are Unicode letters or spaces only; otherwise, returnsfalse. -
Returns
trueif all characters in the current String are Unicode letters or digits only; otherwise, returnsfalse. -
Returns
trueif all characters in the current String are Unicode letters, digits, or spaces only; otherwise, returnsfalse. -
Returns
trueif the current String contains only ASCII printable characters; otherwise, returnsfalse. -
Returns
trueif the specified String is white space, empty (”), or null; otherwise, returnsfalse. -
Returns
trueif the specified String is empty (”) or null; otherwise, returnsfalse. -
Returns
trueif the specified String is not whitespace, not empty (”), and not null; otherwise, returnsfalse. -
Returns
trueif the specified String is not empty (”) and not null; otherwise, returnsfalse. -
Returns
trueif the current String contains only Unicode digits; otherwise, returnsfalse. -
Returns
trueif the current String contains only Unicode digits or spaces; otherwise, returnsfalse. -
Returns
trueif the current String contains only white space characters or is empty; otherwise, returnsfalse. -
Joins the elements of the specified iterable object, such as a List, into a single String separated by the specified separator.
-
Returns the index of the last occurrence of the specified substring. If the substring does not occur, this method returns -1.
-
lastIndexOf(substring, endPosition)
Returns the index of the last occurrence of the specified substring, starting from the character at index 0 and ending at the specified index.
-
Returns the index of the last occurrence of the character that corresponds to the specified character value.
-
lastIndexOfChar(character, endIndex)
Returns the index of the last occurrence of the character that corresponds to the specified character value, starting from the specified index.
-
lastIndexOfIgnoreCase(substring)
Returns the index of the last occurrence of the specified substring regardless of case.
-
lastIndexOfIgnoreCase(substring, endPosition)
Returns the index of the last occurrence of the specified substring regardless of case, starting from the character at index 0 and ending at the specified index.
-
Returns the leftmost characters of the current String of the specified length.
-
Returns the current String padded with spaces on the left and of the specified length.
-
Returns the current String padded with String
padStron the left and of the specified length. -
Returns the number of 16-bit Unicode characters contained in the String.
-
Returns a new String that begins with the character at the specified zero-based startIndex with the number of characters specified by length.
-
Returns the current String with leading, trailing, and repeating white space characters removed.
-
offsetByCodePoints(index, codePointOffset)
Returns the index of the Unicode code point that is offset by the specified number of code points, starting from the given index.
-
Removes all occurrences of the specified substring and returns the String result.
-
Removes the specified substring only if it occurs at the end of the String.
-
removeEndIgnoreCase(substring)
Removes the specified substring only if it occurs at the end of the String using a case-insensitive match.
-
Removes the specified substring only if it occurs at the beginning of the String.
-
removeStartIgnoreCase(substring)
Removes the specified substring only if it occurs at the beginning of the String using a case-insensitive match.
-
Returns the current String repeated the specified number of times.
-
repeat(separator, numberOfTimes)
Returns the current String repeated the specified number of times using the specified separator to separate the repeated Strings.
-
Replaces each substring of a string that matches the literal target sequence target with the specified literal replacement sequence replacement.
-
replaceAll(regExp, replacement)
Replaces each substring of a string that matches the regular expression regExp with the replacement sequence replacement.
-
replaceFirst(regExp, replacement)
Replaces the first substring of a string that matches the regular expression regExp with the replacement sequence replacement.
-
Returns a String with all the characters reversed.
-
Returns the rightmost characters of the current String of the specified length.
-
Returns the current String padded with spaces on the right and of the specified length.
-
Returns the current String padded with String
padStron the right and of the specified length. -
Returns a list that contains each substring of the String that is terminated by either the regular expression regExp or the end of the String.
-
Returns a list that contains each substring of the String that is terminated by either the regular expression regExp or the end of the String.
-
Splits the current String by character type and returns a list of contiguous character groups of the same type as complete tokens.
-
splitByCharacterTypeCamelCase()
Splits the current String by character type and returns a list of contiguous character groups of the same type as complete tokens, with the following exception: the uppercase character, if any, immediately preceding a lowercase character token belongs to the following character token rather than to the preceding.
-
Returns
trueif the String that called the method begins with the specified prefix. -
Returns
trueif the current String begins with the specified prefix regardless of the prefix case. -
Removes HTML markup and returns plain text.
-
Returns a new String that begins with the character at the specified zero-based startIndex and extends to the end of the String.
-
substring(startIndex, endIndex)
Returns a new String that begins with the character at the specified zero-based startIndex and extends to the character at endIndex - 1.
-
Returns the substring that occurs after the first occurrence of the specified separator.
-
Returns the substring that occurs after the last occurrence of the specified separator.
-
Returns the substring that occurs before the first occurrence of the specified separator.
-
substringBeforeLast(separator)
Returns the substring that occurs before the last occurrence of the specified separator.
-
Returns the substring that occurs between two instances of the specified tag String.
-
Returns the substring that occurs between the two specified Strings.
-
Swaps the case of all characters and returns the resulting String by using the default (English US) locale.
-
Substitutes variables in a string for their corresponding values in the
valueMapparameter, and returns the updated string. -
Converts all of the characters in the String to lowercase using the rules of the default (English US) locale.
-
Converts all of the characters in the String to lowercase using the rules of the specified locale.
-
Converts all of the characters in the String to uppercase using the rules of the default (English US) locale.
-
Converts all of the characters in the String to the uppercase using the rules of the specified locale.
-
Returns a copy of the string that no longer contains any leading or trailing white space characters.
-
Returns the current String with the first letter in lowercase.
-
Returns a String representing an unescaped CSV column.
-
Unescapes any EcmaScript literals found in the String.
-
Unescapes the characters in a String using HTML 3.0 entities.
-
Unescapes the characters in a String using HTML 4.0 entities.
-
Returns a String whose Java literals are unescaped. Literals unescaped include escape sequences for quotes (\\”) and control characters, such as tab (\\t), and carriage return (\\n).
-
Returns a String whose escaped Unicode characters are unescaped.
-
Unescapes the characters in a String using XML entities.
-
Returns a String that represents the specified Date in the standard “yyyy-MM-dd” format.
-
Returns a String that represents the specified Datetime in the standard “yyyy-MM-dd HH:mm:ss” format for the local time zone.
-
Returns a String that represents the specified Decimal.
-
Returns a String that represents the specified Double.
-
Returns a String that represents the specified Integer.
-
Returns a String that represents the specified Long.
-
Returns a string representation of the specified object argument.
-
Returns a String that represents the specified Datetime in the standard “yyyy-MM-dd HH:mm:ss” format for the GMT time zone.
Returns an abbreviated version of the String, of the specified length and with ellipses appended if the current String is longer than the specified length; otherwise, returns the original String without ellipses.
public String abbreviate(Integer maxWidth)
-
maxWidth:
Type: Integer
If maxWidth is less than four, this method throws a run-time exception.
Type: String
Returns an abbreviated version of the String, starting at the specified character offset and of the specified length. The returned String has ellipses appended at the start and the end if characters have been removed at these locations.
public String abbreviate(Integer maxWidth, Integer offset)
-
maxWidth:
Type: Integer
Note that the offset is not necessarily the leftmost character in the returned String or the first character following the ellipses, but it appears somewhere in the result. Regardless,
abbreviatewon’t return a String of length greater than maxWidth.If maxWidth is too small, this method throws a run-time exception. -
offset:
Type: Integer
Type: String
Returns the current String with the first letter changed to title case.
public String capitalize()
Type: String
This method is based on the Character.toTitleCase(char) Java method.
Returns a version of the current String of the specified size padded with spaces on the left and right, so that it appears in the center. If the specified size is smaller than the current String size, the entire String is returned without added spaces.
public String center(Integer size)
-
size:
Type: Integer
Type: String
Returns a version of the current String of the specified size padded with the specified String on the left and right, so that it appears in the center. If the specified size is smaller than the current String size, the entire String is returned without padding.
public String center(Integer size, String paddingString)
Type: String
Returns the value of the character at the specified index.
public Integer charAt(Integer index)
-
index:
Type: Integer
The index of the character to get the value of.
Type: Integer
The integer value of the character.
The charAt method returns the value of the character pointed to by the specified index. If the index points to the beginning of a surrogate pair (the high-surrogate code point), this method returns only the high-surrogate code point. To return the supplementary code point corresponding to a surrogate pair, call codePointAt instead.
This example gets the value of the first character at index 0.
This example shows the difference between charAt and codePointAt. The example calls these methods on escaped supplementary Unicode characters. charAt(0) returns the high surrogate value, which corresponds to \uD835. codePointAt(0) returns the value for the entire surrogate pair.
Returns the Unicode code point value at the specified index.
public Integer codePointAt(Integer index)
-
index:
Type: Integer
The index of the characters (Unicode code units) in the string. The index range is from zero to the string length minus one.
Type: Integer
The Unicode code point value at the specified index.
If the index points to the beginning of a surrogate pair (the high-surrogate code point), and the character value at the following index points to the low-surrogate code point, this method returns the supplementary code point corresponding to this surrogate pair. Otherwise, this method returns the character value at the given index.
For more information on Unicode and surrogate pairs, see The Unicode Consortium.
This example gets the code point value of the first character at index 0, which is the escaped Omega character. Also, the example gets the code point at index 20, which corresponds to the escaped supplementary Unicode characters (a pair of characters). Finally, it verifies that the escaped and unescaped forms of Omega have the same code point values.
The supplementary characters in this example (\\uD835\\uDD0A) correspond to mathematical fraktur capital G: 
Returns the Unicode code point value that occurs before the specified index.
public Integer codePointBefore(Integer index)
-
index:
Type: Integer
The index before the Unicode code point that is to be returned. The index range is from one to the string length.
Type: Integer
The character or Unicode code point value that occurs before the specified index.
If the character value at index-1 is the low-surrogate code point, and index-2 is not negative and the character at this index location is the high-surrogate code point, this method returns the supplementary code point corresponding to this surrogate pair. If the character value at index-1 is an unpaired low-surrogate or high-surrogate code point, the surrogate value is returned.
For more information on Unicode and surrogate pairs, see The Unicode Consortium.
This example gets the code point value of the first character (before index 1), which is the escaped Omega character. Also, the example gets the code point at index 20, which corresponds to the escaped supplementary characters (the two characters before index 22).
Returns the number of Unicode code points within the specified text range.
public Integer codePointCount(Integer beginIndex, Integer endIndex)
-
beginIndex:
Type: Integer
The index of the first character in the range.
-
endIndex:
Type: Integer
The index after the last character in the range.
Type: Integer
The number of Unicode code points within the specified range.
The specified range begins at beginIndex and ends at endIndex—1. Unpaired surrogates within the text range count as one code point each.
This example writes the count of code points in a substring that contains an escaped Unicode character and another substring that contains Unicode supplementary characters, which count as one code point.
Compares two strings lexicographically, based on the Unicode value of each character in the Strings.
public Integer compareTo(String secondString)
-
secondString:
Type: String
Type: Integer
The result is:
- A negative Integer if the String that called the method lexicographically precedes secondString
- A positive Integer if the String that called the method lexicographically follows compsecondStringString
- Zero if the Strings are equal
If there is no index position at which the Strings differ, then the shorter String lexicographically precedes the longer String.
Note that this method returns 0 whenever the equals method returns true.
Returns true if and only if the String that called the method contains the specified sequence of characters in substring.
public Boolean contains(String substring)
-
substring:
Type: String
Type: Boolean
Returns true if the current String contains any of the characters in the specified String; otherwise, returns false.
public Boolean containsAny(String inputString)
-
inputString:
Type: String
Type: Boolean
Returns true if the current String contains the specified sequence of characters without regard to case; otherwise, returns false.
public Boolean containsIgnoreCase(String substring)
-
substring:
Type: String
Type: Boolean
Returns true if the current String doesn’t contain any of the characters in the specified String; otherwise, returns false.
public Boolean containsNone(String inputString)
-
inputString:
Type: String
If inputString is an empty string or the current String is empty, this method returns
true. If inputString is null, this method returns a run-time exception.
Type: Boolean
Returns true if the current String contains characters only from the specified sequence of characters and not any other characters; otherwise, returns false.
public Boolean containsOnly(String inputString)
-
inputString:
Type: String
Type: Boolean
Returns true if the current String contains any white space characters; otherwise, returns false.
public Boolean containsWhitespace()
Type: Boolean
Returns the number of times the specified substring occurs in the current String.
public Integer countMatches(String substring)
-
substring:
Type: String
Type: Integer
Returns a version of the current String with all white space characters removed.
public String deleteWhitespace()
Type: String
Returns the difference between the current String and the specified String.
public String difference(String secondString)
-
secondString:
Type: String
If secondString is an empty string, this method returns an empty string.If secondString is null, this method throws a run-time exception.
Type: String
Returns true if the String that called the method ends with the specified suffix.
public Boolean endsWith(String suffix)
-
suffix:
Type: String
Type: Boolean
Returns true if the current String ends with the specified suffix; otherwise, returns false.
public Boolean endsWithIgnoreCase(String suffix)
-
suffix:
Type: String
Type: Boolean
Deprecated. This method is replaced by equals(stringOrId). Returns true if the passed-in string is not null and represents the same binary sequence of characters as the current string. Use this method to perform case-sensitive comparisons.
public Boolean equals(String secondString)
-
secondString:
Type: String
Type: Boolean
This method returns true when the compareTo method returns 0.
Use this method to perform case-sensitive comparisons. In contrast, the == operator performs case-insensitive string comparisons to match Apex semantics.
Returns true if the passed-in object is not null and represents the same binary sequence of characters as the current string. Use this method to compare a string to an object that represents a string or an ID.
public Boolean equals(Object stringOrId)
-
stringOrId:
Type: Object
Type: Boolean
If you compare ID values, the lengths of IDs don’t need to be equal. For example, if you compare a 15-character ID string to an object that represents the equivalent 18-character ID value, this method returns true. For more information about 15-character and 18-character IDs, see the ID Data Type.
Use this method to perform case-sensitive comparisons. In contrast, the == operator performs case-insensitive string comparisons to match Apex semantics.
These examples show comparisons between different types of variables with both equal and unequal values. The examples also show how Apex automatically converts certain values before comparing them.
Returns true if the secondString isn’t null and represents the same sequence of characters as the String that called the method, ignoring case.
public Boolean equalsIgnoreCase(String secondString)
-
secondString:
Type: String
Type: Boolean
The String.equalsIgnoreCase() method ignores the locale of the context user. If you want the string comparison to be performed according to the locale, use the == operator instead. The String.equalsIgnoreCase() method typically executes faster than the operator because the method ignores the locale.
Returns a String for a CSV column enclosed in double quotes, if required.
public String escapeCsv()
Type: String
If the String contains a comma, newline or double quote, the returned String is enclosed in double quotes. Also, any double quote characters in the String are escaped with another double quote.
If the String doesn’t contain a comma, newline or double quote, it is returned unchanged.
Escapes the characters in the String using EcmaScript String rules.
public String escapeEcmaScript()
Type: String
The only difference between Apex strings and EcmaScript strings is that in EcmaScript, a single quote and forward-slash (/) are escaped.
Escapes the characters in a String using HTML 3.0 entities.
public String escapeHtml3()
Type: String
Escapes the characters in a String using HTML 4.0 entities.
public String escapeHtml4()
Type: String
Returns a String whose characters are escaped using Java String rules. Characters escaped include quotes and control characters, such as tab, backslash, and carriage return characters.
public String escapeJava()
Type: String
The escaped string.
Returns a String with the escape character (\) added before any single quotation mark (') or backslash (\) in the String s.
public static String escapeSingleQuotes(String stringToEscape)
-
stringToEscape:
Type: String
Type: String
This method is useful when creating a dynamic SOQL statement to help prevent SOQL injection. See Dynamic SOQL.
Returns a String whose Unicode characters are escaped to a Unicode escape sequence.
public String escapeUnicode()
Type: String
The escaped string.
Escapes the characters in a String using XML entities.
public String escapeXml()
Type: String
Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities. Unicode characters greater than 0x7f are not escaped.
Treat the first argument as a pattern and return a string using the second argument for substitution and formatting. The substitution and formatting are the same as apex:outputText and the Java MessageFormat class. Non-string types in the second argument’s List are implicitly converted to strings, respecting the toString() method overrides that exist on the type.
public static String format(String stringToFormat, List<Object> formattingArguments)
Type: String
From version 51.0 and later, the format() method supports single quotes in the stringToFormat parameter and returns a formatted string using the formattingArguments parameter. In version 50.0 and earlier, single quotes weren’t supported.
Returns a String from the values of the list of integers.
public static String fromCharArray(List<Integer> charArray)
Type: String
Returns an array of character values that represent the characters in this string.
public List<Integer> getChars()
A list of integers, each corresponding to a character value in the string.
This sample converts a string to a character array and then gets the first array element, which corresponds to the value of ‘J’.
If a ”/” (slash) character is present in the string, String.getChars() unescapes it in the returned character array. This example uses the String.escapeJava() method to generate the desired value of ”\\” in the returned string.
Returns the initial sequence of characters as a String that is common to all the specified Strings.
public static String getCommonPrefix(List<String> strings)
Type: String
Returns the Levenshtein distance between the current String and the specified String.
public Integer getLevenshteinDistance(String stringToCompare)
-
stringToCompare:
Type: String
Type: Integer
The Levenshtein distance is the number of changes needed to change one String into another. Each change is a single character modification (deletion, insertion or substitution).
Returns the Levenshtein distance between the current String and the specified String if it is less than or equal than the given threshold; otherwise, returns -1.
public Integer getLevenshteinDistance(String stringToCompare, Integer threshold)
Type: Integer
The Levenshtein distance is the number of changes needed to change one String into another. Each change is a single character modification (deletion, insertion or substitution).
Example:
In this example, the Levenshtein distance is 3, but the threshold argument is 2, which is less than the distance, so this method returns -1.
Returns a hash code value for this string.
public Integer hashCode()
Type: Integer
This value is based on the hash code computed by the Java String.hashCode counterpart method.
You can use this method to simplify the computation of a hash code for a custom type that contains String member variables. You can compute your type’s hash code value based on the hash code of each String variable. For example:
For more details about the use of hash code methods with custom types, see Using Custom Types in Map Keys and Sets.
Returns the index of the first occurrence of the specified substring. If the substring does not occur, this method returns -1.
public Integer indexOf(String substring)
-
substring:
Type: String
Type: Integer
Returns the zero-based index of the first occurrence of the specified substring from the point of the given index. If the substring does not occur, this method returns -1.
public Integer indexOf(String substring, Integer index)
Type: Integer
Returns the zero-based index of the first occurrence of any character specified in the substring. If none of the characters occur, returns -1.
public Integer indexOfAny(String substring)
-
substring:
Type: String
Type: Integer
Returns the zero-based index of the first occurrence of a character that is not in the specified substring. Otherwise, returns -1.
public Integer indexOfAnyBut(String substring)
-
substring:
Type: String
Type: Integer
Returns the index of the first occurrence of the character that corresponds to the specified character value.
public Integer indexOfChar(Integer character)
-
character:
Type: Integer
The integer value of the character in the string.
Type: Integer
The index of the first occurrence of the specified character, -1 if the character is not found.
The index that this method returns is in Unicode code units.
Returns the index of the first occurrence of the character that corresponds to the specified character value, starting from the specified index.
public Integer indexOfChar(Integer character, Integer startIndex)
-
character:
Type: Integer
The integer value of the character to look for.
-
startIndex:
Type: Integer
The index to start the search from.
Type: Integer
The index, starting from the specified start index, of the first occurrence of the specified character, -1 if the character is not found.
The index that this method returns is in Unicode code units.
This example shows different ways of searching for the index of the Omega character. The first call to indexOfChar doesn’t specify a start index and therefore the returned index is 0, which is the first occurrence of Omega in the entire string. The subsequent calls specify a start index to find the occurrence of Omega in substrings that start at the specified index.
Returns the zero-based index of the character where the current String begins to differ from the specified String.
public Integer indexOfDifference(String stringToCompare)
-
stringToCompare:
Type: String
Type: Integer
Returns the zero-based index of the first occurrence of the specified substring without regard to case. If the substring does not occur, this method returns -1.
public Integer indexOfIgnoreCase(String substring)
-
substring:
Type: String
Type: Integer
Returns the zero-based index of the first occurrence of the specified substring from the point of index i, without regard to case. If the substring does not occur, this method returns -1.
public Integer indexOfIgnoreCase(String substring, Integer startPosition)
Type: Integer
Returns true if all characters in the current String are lowercase; otherwise, returns false.
public Boolean isAllLowerCase()
Type: Boolean
Returns true if all characters in the current String are uppercase; otherwise, returns false.
public Boolean isAllUpperCase()
Type: Boolean
Returns true if all characters in the current String are Unicode letters only; otherwise, returns false.
public Boolean isAlpha()
Type: Boolean
Returns true if all characters in the current String are Unicode letters or spaces only; otherwise, returns false.
public Boolean isAlphaSpace()
Type: Boolean
Returns true if all characters in the current String are Unicode letters or digits only; otherwise, returns false.
public Boolean isAlphanumeric()
Type: Boolean
Unicode Roman numerals are classified as a type of number form, not a type of digit. Therefore, the isAlphanumeric() method returns false if used on a String that contains a Roman numeral. For Unicode classifications, see the Unicode Character Code Charts.
Returns true if all characters in the current String are Unicode letters, digits, or spaces only; otherwise, returns false.
public Boolean isAlphanumericSpace()
Type: Boolean
Unicode Roman numerals are classified as a type of number form, not a type of digit. Therefore, the isAlphanumericSpace() method returns false if used on a String that contains a Roman numeral. For Unicode classifications, see the Unicode Character Code Charts.
Returns true if the current String contains only ASCII printable characters; otherwise, returns false.
public Boolean isAsciiPrintable()
Type: Boolean
Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.
public static Boolean isBlank(String inputString)
-
inputString:
Type: String
Type: Boolean
Returns true if the specified String is empty ('') or null; otherwise, returns false.
public static Boolean isEmpty(String inputString)
-
inputString:
Type: String
Type: Boolean
Returns true if the specified String is not whitespace, not empty (''), and not null; otherwise, returns false.
public static Boolean isNotBlank(String inputString)
-
inputString:
Type: String
Type: Boolean
Returns true if the specified String is not empty ('') and not null; otherwise, returns false.
public static Boolean isNotEmpty(String inputString)
-
inputString:
Type: String
Type: Boolean
Returns true if the current String contains only Unicode digits; otherwise, returns false.
public Boolean isNumeric()
Type: Boolean
A decimal point (1.2) is not a Unicode digit.
Returns true if the current String contains only Unicode digits or spaces; otherwise, returns false.
public Boolean isNumericSpace()
Type: Boolean
A decimal point (1.2) is not a Unicode digit.
Returns true if the current String contains only white space characters or is empty; otherwise, returns false.
public Boolean isWhitespace()
Type: Boolean
Joins the elements of the specified iterable object, such as a List, into a single String separated by the specified separator.
public static String join(Object iterableObj, String separator)
-
iterableObj:
Type: Object
-
separator:
Type: String
Type: String
Returns the index of the last occurrence of the specified substring. If the substring does not occur, this method returns -1.
public Integer lastIndexOf(String substring)
-
substring:
Type: String
Type: Integer
Returns the index of the last occurrence of the specified substring, starting from the character at index 0 and ending at the specified index.
public Integer lastIndexOf(String substring, Integer endPosition)
Type: Integer
If the substring doesn’t occur or endPosition is negative, this method returns -1. If endPosition is larger than the last index in the current String, the entire String is searched.
Returns the index of the last occurrence of the character that corresponds to the specified character value.
public Integer lastIndexOfChar(Integer character)
-
character:
Type: Integer
The integer value of the character in the string.
Type: Integer
The index of the last occurrence of the specified character, -1 if the character is not found.
The index that this method returns is in Unicode code units.
Returns the index of the last occurrence of the character that corresponds to the specified character value, starting from the specified index.
public Integer lastIndexOfChar(Integer character, Integer endIndex)
-
character:
Type: Integer
The integer value of the character to look for.
-
endIndex:
Type: Integer
The index to end the search at.
Type: Integer
The index, starting from the specified start index, of the last occurrence of the specified character. -1 if the character is not found.
The index that this method returns is in Unicode code units.
This example shows different ways of searching for the index of the last occurrence of the Omega character. The first call to lastIndexOfChar doesn’t specify an end index and therefore the returned index is 12, which is the last occurrence of Omega in the entire string. The subsequent calls specify an end index to find the last occurrence of Omega in substrings.
Returns the index of the last occurrence of the specified substring regardless of case.
public Integer lastIndexOfIgnoreCase(String substring)
-
substring:
Type: String
Type: Integer
If the substring doesn’t occur, this method returns -1.
Returns the index of the last occurrence of the specified substring regardless of case, starting from the character at index 0 and ending at the specified index.
public Integer lastIndexOfIgnoreCase(String substring, Integer endPosition)
Type: Integer
If the substring doesn’t occur or endPosition is negative, this method returns -1. If endPosition is larger than the last index in the current String, the entire String is searched.
Returns the leftmost characters of the current String of the specified length.
public String left(Integer length)
-
length:
Type: Integer
Type: String
If length is greater than the String size, the entire String is returned.
Returns the current String padded with spaces on the left and of the specified length.
public String leftPad(Integer length)
-
length:
Type: Integer
If length is less than or equal to the current String size, the entire String is returned without space padding.
Type: String
Returns the current String padded with String padStr on the left and of the specified length.
public String leftPad(Integer length, String padStr)
-
length:
Type: Integer
-
padStr:
Type: String
String to pad with;
if null or empty treated as single blank.
If length is less than or equal to the current String size, the entire String is returned without space padding.
Type: String
Returns the number of 16-bit Unicode characters contained in the String.
public Integer length()
Type: Integer
Returns a new String that begins with the character at the specified zero-based startIndex with the number of characters specified by length.
public String mid(Integer startIndex, Integer length)
-
startIndex:
Type: Integer
If startIndex is negative, it is considered to be zero.
-
length:
Type: Integer
If length is negative or zero, an empty String is returned. If length is greater than the remaining characters, the remainder of the String is returned.
Type: String
This method is similar to the substring(startIndex) and substring(startIndex, endIndex) methods, except that the second argument is the number of characters to return.
Returns the current String with leading, trailing, and repeating white space characters removed.
public String normalizeSpace()
Type: String
This method normalizes the following white space characters: space, tab (\t), new line (\n), carriage return (\r), and form feed (\f).
Returns the index of the Unicode code point that is offset by the specified number of code points, starting from the given index.
public Integer offsetByCodePoints(Integer index, Integer codePointOffset)
-
index:
Type: Integer
The start index in the string.
-
codePointOffset:
Type: Integer
The number of code points to be offset.
Type: Integer
The index that corresponds to the start index that is added to the offset.
Unpaired surrogates within the text range that is specified by index and codePointOffset count as one code point each.
This example calls offsetByCodePoints on a string with a start index of 0 (to start from the first character) and an offset of three code points. The string contains one sequence of supplementary characters in escaped form (a pair of characters). After an offset of three code points when counting from the beginning of the string, the returned code point index is four.
Removes all occurrences of the specified substring and returns the String result.
public String remove(String substring)
-
substring:
Type: String
Type: String
Removes the specified substring only if it occurs at the end of the String.
public String removeEnd(String substring)
-
substring:
Type: String
Type: String
Removes the specified substring only if it occurs at the end of the String using a case-insensitive match.
public String removeEndIgnoreCase(String substring)
-
substring:
Type: String
Type: String
Removes the specified substring only if it occurs at the beginning of the String.
public String removeStart(String substring)
-
substring:
Type: String
Type: String
Removes the specified substring only if it occurs at the beginning of the String using a case-insensitive match.
public String removeStartIgnoreCase(String substring)
-
substring:
Type: String
Type: String
Returns the current String repeated the specified number of times.
public String repeat(Integer numberOfTimes)
-
numberOfTimes:
Type: Integer
Type: String
Returns the current String repeated the specified number of times using the specified separator to separate the repeated Strings.
public String repeat(String separator, Integer numberOfTimes)
Type: String
Replaces each substring of a string that matches the literal target sequence target with the specified literal replacement sequence replacement.
public String replace(String target, String replacement)
Type: String
Replaces each substring of a string that matches the regular expression regExp with the replacement sequence replacement.
public String replaceAll(String regExp, String replacement)
Type: String
See the Java Pattern class for information on regular expressions.
Replaces the first substring of a string that matches the regular expression regExp with the replacement sequence replacement.
public String replaceFirst(String regExp, String replacement)
Type: String
See the Java Pattern class for information on regular expressions.
Returns a String with all the characters reversed.
public String reverse()
Type: String
Returns the rightmost characters of the current String of the specified length.
public String right(Integer length)
-
length:
Type: Integer
If length is greater than the String size, the entire String is returned.
Type: String
Returns the current String padded with spaces on the right and of the specified length.
public String rightPad(Integer length)
-
length:
Type: Integer
If length is less than or equal to the current String size, the entire String is returned without space padding.
Type: String
Returns the current String padded with String padStr on the right and of the specified length.
public String rightPad(Integer length, String padStr)
-
length:
Type: Integer
-
padStr:
Type: String
String to pad with;
if null or empty treated as single blank.
If length is less than or equal to the current String size, the entire String is returned without space padding.
Type: String
Returns a list that contains each substring of the String that is terminated by either the regular expression regExp or the end of the String.
public String[] split(String regExp)
-
regExp:
Type: String
Type: String[]
In API version 34.0 and earlier, a zero-width regExp value produces an empty list item at the beginning of the method’s output.
See the Java Pattern class for information on regular expressions.
The substrings are placed in the list in the order in which they occur in the String. If regExp does not match any part of the String, the resulting list has just one element containing the original String.
In the following example, a string is split using a backslash as a delimiter.
Returns a list that contains each substring of the String that is terminated by either the regular expression regExp or the end of the String.
public String[] split(String regExp, Integer limit)
Type: String[]
In API version 34.0 and earlier, a zero-width regExp value produces an empty list item at the beginning of the method’s output.
The optional limit parameter controls the number of times the pattern is applied and therefore affects the length of the list.
-
If limit is greater than zero:
- The pattern is applied a maximum of (limit – 1) times.
- The list’s length is no greater than limit.
- The list’s last entry contains all input beyond the last matched delimiter.
-
If limit is non-positive, the pattern is applied as many times as possible, and the list can have any length.
-
If limit is zero, the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.
For example, for String s = 'boo:and:moo':
s.split(':', 2)results in{'boo', 'and:moo'}s.split(':', 5)results in{'boo', 'and', 'moo'}s.split(':', -2)results in{'boo', 'and', 'moo'}s.split('o', 5)results in{'b', '', ':and:m', '', ''}s.split('o', -2)results in{'b', '', ':and:m', '', ''}s.split('o', 0)results in{'b', '', ':and:m'}
Splits the current String by character type and returns a list of contiguous character groups of the same type as complete tokens.
public List<String> splitByCharacterType()
For more information about the character types used, see java.lang.Character.getType(char).
Splits the current String by character type and returns a list of contiguous character groups of the same type as complete tokens, with the following exception: the uppercase character, if any, immediately preceding a lowercase character token belongs to the following character token rather than to the preceding.
public List<String> splitByCharacterTypeCamelCase()
For more information about the character types used, see java.lang.Character.getType(char).
Returns true if the String that called the method begins with the specified prefix.
public Boolean startsWith(String prefix)
-
prefix:
Type: String
Type: Boolean
Returns true if the current String begins with the specified prefix regardless of the prefix case.
public Boolean startsWithIgnoreCase(String prefix)
-
prefix:
Type: String
Type: Boolean
Removes HTML markup and returns plain text.
public String stripHtmlTags()
Type: String
The stripHtmlTags function doesn’t recursively strip tags; therefore, tags can still exist in the returned string. Don’t use the stripHtmlTags function to sanitize input for inclusion as a raw HTML page. The unescaped output isn’t considered safe to include in an HTML document. The function will be deprecated in a future release.
Returns a new String that begins with the character at the specified zero-based startIndex and extends to the end of the String.
public String substring(Integer startIndex)
-
startIndex:
Type: Integer
Type: String
Returns a new String that begins with the character at the specified zero-based startIndex and extends to the character at endIndex - 1.
public String substring(Integer startIndex, Integer endIndex)
Type: String
Returns the substring that occurs after the first occurrence of the specified separator.
public String substringAfter(String separator)
-
separator:
Type: String
Type: String
Returns the substring that occurs after the last occurrence of the specified separator.
public String substringAfterLast(String separator)
-
separator:
Type: String
Type: String
Returns the substring that occurs before the first occurrence of the specified separator.
public String substringBefore(String separator)
-
separator:
Type: String
Type: String
Returns the substring that occurs before the last occurrence of the specified separator.
public String substringBeforeLast(String separator)
-
separator:
Type: String
Type: String
Returns the substring that occurs between two instances of the specified tag String.
public String substringBetween(String tag)
-
tag:
Type: String
Type: String
Returns the substring that occurs between the two specified Strings.
public String substringBetween(String open, String close)
Type: String
Swaps the case of all characters and returns the resulting String by using the default (English US) locale.
public String swapCase()
Type: String
Upper case and title case converts to lower case, and lower case converts to upper case.
Substitutes variables in a string for their corresponding values in the valueMap parameter, and returns the updated string.
public String template(Map<String, Object> valueMap)
-
valueMap:
Type: Map<String,Object>
A map in which each key is the variable name specified in the string, and each value is the corresponding value to substitute into the string.
Type: String
Both regular and multiline string literals are supported. In the string literal, use the syntax ${variableName} to represent a placeholder variable. For example:
To include literal text between ${} in the string, use the $ character to escape the variable reference. For example:
Non-string values in the valueMap are implicitly converted to strings by using the toString() method overrides that exist on the type.
Default values for missing variables aren’t supported. If a variable is present in the string but not as a key in the valueMap, a StringException is thrown.
Implicit string formatting lookups, such as date formatting, also aren’t supported as values. Instead, first use the relevant Apex method to apply the formatting, and then pass the result into the valueMap.
In this example, the multiline string jsonBody has two variables: sc and sn. The template method sets the variables to the values of schoolCity and schoolName, respectively, and returns the updated string.
Converts all of the characters in the String to lowercase using the rules of the default (English US) locale.
public String toLowerCase()
Type: String
Converts all of the characters in the String to lowercase using the rules of the specified locale.
public String toLowerCase(String locale)
-
locale:
Type: String
Type: String
Converts all of the characters in the String to uppercase using the rules of the default (English US) locale.
public String toUpperCase()
Type: String
Converts all of the characters in the String to the uppercase using the rules of the specified locale.
public String toUpperCase(String locale)
-
locale:
Type: String
Type: String
Returns a copy of the string that no longer contains any leading or trailing white space characters.
public String trim()
Type: String
Leading and trailing ASCII control characters such as tabs and newline characters are also removed. White space and control characters that aren’t at the beginning or end of the sentence aren’t removed.
Returns the current String with the first letter in lowercase.
public String uncapitalize()
Type: String
Returns a String representing an unescaped CSV column.
public String unescapeCsv()
Type: String
If the String is enclosed in double quotes and contains a comma, newline or double quote, quotes are removed. Also, any double quote escaped characters (a pair of double quotes) are unescaped to just one double quote.
If the String is not enclosed in double quotes, or is and does not contain a comma, newline or double quote, it is returned unchanged.
Unescapes any EcmaScript literals found in the String.
public String unescapeEcmaScript()
Type: String
Unescapes the characters in a String using HTML 3.0 entities.
public String unescapeHtml3()
Type: String
Unescapes the characters in a String using HTML 4.0 entities.
public String unescapeHtml4()
Type: String
If an entity isn’t recognized, it is kept as is in the returned string.
Returns a String whose Java literals are unescaped. Literals unescaped include escape sequences for quotes (\\”) and control characters, such as tab (\\t), and carriage return (\\n).
public String unescapeJava()
Type: String
The unescaped string.
Returns a String whose escaped Unicode characters are unescaped.
public String unescapeUnicode()
Type: String
The unescaped string.
Unescapes the characters in a String using XML entities.
public String unescapeXml()
Type: String
Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.
Returns a String that represents the specified Date in the standard “yyyy-MM-dd” format.
public static String valueOf(Date dateToConvert)
-
dateToConvert:
Type: Date
Type: String
Returns a String that represents the specified Datetime in the standard “yyyy-MM-dd HH:mm:ss” format for the local time zone.
public static String valueOf(Datetime datetimeToConvert)
-
datetimeToConvert:
Type: Datetime
Type: String
Returns a String that represents the specified Decimal.
public static String valueOf(Decimal decimalToConvert)
-
decimalToConvert:
Type: Decimal
Type: String
Returns a String that represents the specified Double.
public static String valueOf(Double doubleToConvert)
-
doubleToConvert:
Type: Double
Type: String
Returns a String that represents the specified Integer.
public static String valueOf(Integer integerToConvert)
-
integerToConvert:
Type: Integer
Type: String
Returns a String that represents the specified Long.
public static String valueOf(Long longToConvert)
-
longToConvert:
Type: Long
Type: String
Returns a string representation of the specified object argument.
public static String valueOf(Object toConvert)
-
toConvert:
Type: Object
Type: String
If the argument is not a String, the valueOf method converts it into a String by calling the toString method on the argument, if available, or any overridden toString method if the argument is a user-defined type. Otherwise, if no toString method is available, it returns a String representation of the argument.
Returns a String that represents the specified Datetime in the standard “yyyy-MM-dd HH:mm:ss” format for the GMT time zone.
public static String valueOfGmt(Datetime datetimeToConvert)
-
datetimeToConvert:
Type: Datetime
Type: String