For more information on Strings, see Primitive Data Types.
The following are methods for String.
public String abbreviate(Integer maxWidth)
Type: String
String s = 'Hello Maximillian'; String s2 = s.abbreviate(8); System.assertEquals('Hello...', s2); System.assertEquals(8, s2.length());
public String abbreviate(Integer maxWidth, Integer offset)
Type: String
String s = 'Hello Maximillian'; // Start at M String s2 = s.abbreviate(9,6); System.assertEquals('...Max...', s2); System.assertEquals(9, s2.length());
public String capitalize()
Type: String
This method is based on the Character.toTitleCase(char) Java method.
String s = 'hello maximillian'; String s2 = s.capitalize(); System.assertEquals('Hello maximillian', s2);
public String center(Integer size)
Type: String
String s = 'hello'; String s2 = s.center(9); System.assertEquals( ' hello ', s2);
public String center(Integer size, String paddingString)
Type: String
String s = 'hello'; String s2 = s.center(9, '-'); System.assertEquals('--hello--', s2);
public Integer charAt(Integer index)
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.
String str = 'Ω is Omega.'; System.assertEquals(937, str.charAt(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.
String str = '\uD835\uDD0A'; System.assertEquals(55349, str.charAt(0), 'charAt(0) didn\'t return the high surrogate.'); System.assertEquals(120074, str.codePointAt(0), 'codePointAt(0) didn\'t return the entire two-character supplementary value.');
public Integer codePointAt(Integer 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:
String str = '\u03A9 is Ω (Omega), and \uD835\uDD0A ' + ' is Fraktur Capital G.'; System.assertEquals(937, str.codePointAt(0)); System.assertEquals(120074, str.codePointAt(20)); // Escaped or unescaped forms of the same character have the same code point System.assertEquals(str.codePointAt(0), str.codePointAt(5));
public Integer codePointBefore(Integer index)
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).
String str = '\u03A9 is Ω (Omega), and \uD835\uDD0A ' + ' is Fraktur Capital G.'; System.assertEquals(937, str.codePointBefore(1)); System.assertEquals(120074, str.codePointBefore(22));
public Integer codePointCount(Integer beginIndex, Integer endIndex)
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.
String str = '\u03A9 and \uD835\uDD0A characters.'; System.debug('Count of code points for ' + str.substring(0,1) + ': ' + str.codePointCount(0,1)); System.debug('Count of code points for ' + str.substring(6,8) + ': ' + str.codePointCount(6,8)); // Output: // Count of code points for Ω: 1 // Count of code points for 𝔊: 1
public Integer compareTo(String secondString)
Type: Integer
The result is:
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.
String myString1 = 'abcde'; String myString2 = 'abcd'; Integer result = myString1.compareTo(myString2); System.assertEquals(result, 1);
public Boolean contains(String substring)
Type: Boolean
String myString1 = 'abcde'; String myString2 = 'abcd'; Boolean result = myString1.contains(myString2); System.assertEquals(result, true);
public Boolean containsAny(String inputString)
Type: Boolean
String s = 'hello'; Boolean b1 = s.containsAny('hx'); Boolean b2 = s.containsAny('x'); System.assertEquals(true, b1); System.assertEquals(false, b2);
public Boolean containsIgnoreCase(String substring)
Type: Boolean
String s = 'hello'; Boolean b = s.containsIgnoreCase('HE'); System.assertEquals( true, b);
public Boolean containsNone(String substring)
Type: Boolean
String s1 = 'abcde'; System.assert(s1.containsNone('fg'));
public Boolean containsOnly(String inputString)
Type: Boolean
String s1 = 'abba'; String s2 = 'abba xyz'; Boolean b1 = s1.containsOnly('abcd'); System.assertEquals( true, b1); Boolean b2 = s2.containsOnly('abcd'); System.assertEquals( false, b2);
public Boolean containsWhitespace()
Type: Boolean
String s = 'Hello Jane'; System.assert(s.containsWhitespace()); //true s = 'HelloJane '; System.assert(s.containsWhitespace()); //true s = ' HelloJane'; System.assert(s.containsWhitespace()); //true s = 'HelloJane'; System.assert(!s.containsWhitespace()); //false
public Integer countMatches(String substring)
Type: Integer
String s = 'Hello Jane'; System.assertEquals(1, s.countMatches('Hello')); s = 'Hello Hello'; System.assertEquals(2, s.countMatches('Hello')); s = 'Hello hello'; System.assertEquals(1, s.countMatches('Hello'));
public String deleteWhitespace()
Type: String
String s1 = ' Hello Jane '; String s2 = 'HelloJane'; System.assertEquals(s2, s1.deleteWhitespace());
public String difference(String secondString)
Type: String
String s = 'Hello Jane'; String d1 = s.difference('Hello Max'); System.assertEquals( 'Max', d1); String d2 = s.difference('Goodbye'); System.assertEquals( 'Goodbye', d2);
public Boolean equals(String secondString)
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.
String myString1 = 'abcde'; String myString2 = 'abcd'; Boolean result = myString1.equals(myString2); System.assertEquals(result, false);
public Boolean equals(Object stringOrId)
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.
// Compare a string to an object containing a string Object obj1 = 'abc'; String str = 'abc'; Boolean result1 = str.equals(obj1); System.assertEquals(true, result1); // Compare a string to an object containing a number Integer obj2 = 100; Boolean result2 = str.equals(obj2); System.assertEquals(false, result2); // Compare a string to an ID of the same length. // 15-character ID Id idValue15 = '001D000000Ju1zH'; // 15-character ID string value String stringValue15 = '001D000000Ju1zH'; Boolean result3 = stringValue15.equals(IdValue15); System.assertEquals(true, result3); // Compare two equal ID values of different lengths: // 15-character ID and 18-character ID Id idValue18 = '001D000000Ju1zHIAR'; Boolean result4 = stringValue15.equals(IdValue18); System.assertEquals(true, result4);
public Boolean equalsIgnoreCase(String secondString)
Type: Boolean
String myString1 = 'abcd'; String myString2 = 'ABCD'; Boolean result = myString1.equalsIgnoreCase(myString2); System.assertEquals(result, true);
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.
String s1 = 'Max1, "Max2"'; String s2 = s1.escapeCsv(); System.assertEquals('"Max1, ""Max2"""', s2);
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.
String s1 = '"grade": 3.9/4.0'; String s2 = s1.escapeEcmaScript(); System.debug(s2); // Output is: // \"grade\": 3.9\/4.0 System.assertEquals( '\\"grade\\": 3.9\\/4.0', s2);
public String escapeHtml3()
Type: String
String s1 = '"<Black&White>"'; String s2 = s1.escapeHtml3(); System.debug(s2); // Output: // "<Black& // White>"
public String escapeHtml4()
Type: String
String s1 = '"<Black&White>"'; String s2 = s1.escapeHtml4(); System.debug(s2); // Output: // "<Black& // White>"
public String escapeJava()
// Input string contains quotation marks String s = 'Company: "Salesforce.com"'; String escapedStr = s.escapeJava(); // Output string has the quotes escpaded System.assertEquals('Company: \\"Salesforce.com\\"', escapedStr);
public static String escapeSingleQuotes(String stringToEscape)
Type: String
This method is useful when creating a dynamic SOQL statement, to help prevent SOQL injection. For more information on dynamic SOQL, see Dynamic SOQL.
String s = '\'Hello Jason\''; system.debug(s); // Outputs 'Hello Jason' String escapedStr = String.escapeSingleQuotes(s); // Outputs \'Hello Jason\' system.debug(escapedStr); // Escapes the string \\\' to string \' system.assertEquals('\\\'Hello Jason\\\'', escapedStr);
public String escapeUnicode()
String s = 'De onde você é?'; String escapedStr = s.escapeUnicode(); System.assertEquals('De onde voc\\u00EA \\u00E9?', escapedStr);
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.
String s1 = '"<Black&White>"'; String s2 = s1.escapeXml(); System.debug(s2); // Output: // "<Black& // White>"
public static String format(String stringToFormat, List<String> formattingArguments)
Type: String
String placeholder = 'Hello {0}, {1} is cool!'; List<String> fillers = new String[]{'Jason','Apex'}; String formatted = String.format(placeholder, fillers); System.assertEquals('Hello Jason, Apex is cool!', formatted);
public static String fromCharArray(List<Integer> charArray)
Type: String
List<Integer> charArr= new Integer[]{74}; String convertedChar = String.fromCharArray(charArr); System.assertEquals('J', convertedChar);
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'.
String str = 'Jane goes fishing.'; Integer[] chars = str.getChars(); // Get the value of 'J' System.assertEquals(74, chars[0]);
public static String getCommonPrefix(List<String> strings)
Type: String
List<String> ls = new List<String>{'SFDCApex', 'SFDCVisualforce'}; String prefix = String.getCommonPrefix(ls); System.assertEquals('SFDC', prefix);
public Integer getLevenshteinDistance(String stringToCompare)
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).
String s = 'Hello Joe'; Integer i = s.getLevenshteinDistance('Hello Max'); System.assertEquals(3, i);
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.
String s = 'Hello Jane'; Integer i = s.getLevenshteinDistance('Hello Max', 2); System.assertEquals(-1, i);
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.
public class MyCustomClass { String x,y; // Provide a custom hash code public Integer hashCode() { return (31*x.hashCode())^(y.hashCode()); } }
public Integer indexOf(String substring)
Type: Integer
String myString1 = 'abcde'; String myString2 = 'cd'; Integer result = myString1.indexOf(mystring2); System.assertEquals(2, result);
public Integer indexOf(String substring, Integer index)
Type: Integer
String myString1 = 'abcdabcd'; String myString2 = 'ab'; Integer result = myString1.indexOf(mystring2, 1); System.assertEquals(4, result);
public Integer indexOfAny(String substring)
Type: Integer
String s1 = 'abcd'; String s2 = 'xc'; Integer result = s1.indexOfAny(s2); System.assertEquals(2, result);
public Integer indexOfAnyBut(String substring)
Type: Integer
String s1 = 'abcd'; String s2 = 'xc'; Integer result = s1.indexOfAnyBut(s2); System.assertEquals(0, result);
public Integer indexOfChar(Integer character)
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.
String str = '\\u03A9 is Ω (Omega)'; // Returns 0, which is the first character. System.debug('indexOfChar(937)=' + str.indexOfChar(937)); // Output: // indexOfChar(937)=0
public Integer indexOfChar(Integer character, Integer startIndex)
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.
String str = 'Ω and \\u03A9 and Ω'; System.debug('indexOfChar(937)=' + str.indexOfChar(937)); System.debug('indexOfChar(937,1)=' + str.indexOfChar(937,1)); System.debug('indexOfChar(937,10)=' + str.indexOfChar(937,10)); // Output: // indexOfChar(937)=0 // indexOfChar(937,1)=6, (corresponds to the escaped form \\u03A9) // indexOfChar(937,10)=12
public Integer indexOfDifference(String stringToCompare)
Type: Integer
String s1 = 'abcd'; String s2 = 'abxc'; Integer result = s1.indexOfDifference(s2); System.assertEquals(2, result);
public Integer indexOfIgnoreCase(String substring)
Type: Integer
String s1 = 'abcd'; String s2 = 'BC'; Integer result = s1.indexOfIgnoreCase(s2, 0); System.assertEquals(1, result);
public Integer indexOfIgnoreCase(String substring, Integer startPosition)
Type: Integer
public Boolean isAllLowerCase()
Type: Boolean
String allLower = 'abcde'; System.assert(allLower.isAllLowerCase());
public Boolean isAllUpperCase()
Type: Boolean
String allUpper = 'ABCDE'; System.assert(allUpper.isAllUpperCase());
public Boolean isAlpha()
Type: Boolean
// Letters only String s1 = 'abc'; // Returns true Boolean b1 = s1.isAlpha(); System.assertEquals( true, b1); // Letters and numbers String s2 = 'abc 21'; // Returns false Boolean b2 = s2.isAlpha(); System.assertEquals( false, b2);
public Boolean isAlphaSpace()
Type: Boolean
String alphaSpace = 'aA Bb'; System.assert(alphaSpace.isAlphaSpace()); String notAlphaSpace = 'ab 12'; System.assert(!notAlphaSpace.isAlphaSpace()); notAlphaSpace = 'aA$Bb'; System.assert(!notAlphaSpace.isAlphaSpace());
public Boolean isAlphanumeric()
Type: Boolean
// Letters only String s1 = 'abc'; // Returns true Boolean b1 = s1.isAlphanumeric(); System.assertEquals( true, b1); // Letters and numbers String s2 = 'abc021'; // Returns true Boolean b2 = s2.isAlphanumeric(); System.assertEquals( true, b2);
public Boolean isAlphanumericSpace()
Type: Boolean
String alphanumSpace = 'AE 86'; System.assert(alphanumSpace.isAlphanumericSpace()); String notAlphanumSpace = 'aA$12'; System.assert(!notAlphanumSpace.isAlphaSpace());
public Boolean isAsciiPrintable()
Type: Boolean
String ascii = 'abcd1234!@#$%^&*()`~-_+={[}]|:<,>.?'; System.assert(ascii.isAsciiPrintable()); String notAscii = '√'; System.assert(!notAscii.isAsciiPrintable());
public static Boolean isBlank(String inputString)
Type: Boolean
String blank = ''; String nullString = null; String whitespace = ' '; System.assert(String.isBlank(blank)); System.assert(String.isBlank(nullString)); System.assert(String.isBlank(whitespace)); String alpha = 'Hello'; System.assert(!String.isBlank(alpha));
public static Boolean isEmpty(String inputString)
Type: Boolean
String empty = ''; String nullString = null; System.assert(String.isEmpty(empty)); System.assert(String.isEmpty(nullString)); String whitespace = ' '; String alpha = 'Hello'; System.assert(!String.isEmpty(whitespace)); System.assert(!String.isEmpty(alpha));
public static Boolean isNotBlank(String inputString)
Type: Boolean
String alpha = 'Hello world!'; System.assert(String.isNotBlank(alpha)); String blank = ''; String nullString = null; String whitespace = ' '; System.assert(!String.isNotBlank(blank)); System.assert(!String.isNotBlank(nullString)); System.assert(!String.isNotBlank(whitespace));
public static Boolean isNotEmpty(String inputString)
Type: Boolean
String whitespace = ' '; String alpha = 'Hello world!'; System.assert(String.isNotEmpty(whitespace)); System.assert(String.isNotEmpty(alpha)); String empty = ''; String nullString = null; System.assert(!String.isNotEmpty(empty)); System.assert(!String.isNotEmpty(nullString));
public Boolean isNumeric()
Type: Boolean
String numeric = '1234567890'; System.assert(numeric.isNumeric()); String alphanumeric = 'R32'; String decimalPoint = '1.2'; System.assert(!alphanumeric.isNumeric()); System.assert(!decimalpoint.isNumeric());
public Boolean isNumericSpace()
Type: Boolean
A decimal point (1.2) is not a Unicode digit.
String numericSpace = '1 2 3'; System.assert(numericSpace.isNumericspace()); String notNumericspace = 'FD3S FC3S'; System.assert(!notNumericspace.isNumericspace());
public Boolean isWhitespace()
Type: Boolean
String whitespace = ' '; String blank = ''; System.assert(whitespace.isWhitespace()); System.assert(blank.isWhitespace()); String alphanum = 'SIL80'; System.assert(!alphanum.isWhitespace());
public static String join(Object iterableObj, String separator)
Type: String
List<Integer> li = new List<Integer> {10, 20, 30}; String s = String.join( li, '/'); System.assertEquals( '10/20/30', s);
public Integer lastIndexOf(String substring)
Type: Integer
String s1 = 'abcdefgc'; Integer i1 = s1.lastIndexOf('c'); System.assertEquals(7, i1);
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.
String s1 = 'abcdaacd'; Integer i1 = s1.lastIndexOf('c', 7); System.assertEquals( 6, i1); Integer i2 = s1.lastIndexOf('c', 3); System.assertEquals( 2, i2);
public Integer indexOfChar(Integer character)
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.
String str = '\u03A9 is Ω (Omega)'; // Get the last occurrence of Omega. System.assertEquals(5, str.lastIndexOfChar(937));
public Integer lastIndexOfChar(Integer character, Integer endIndex)
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.
String str = 'Ω and \u03A9 and Ω'; System.assertEquals(12, str.lastIndexOfChar(937)); System.assertEquals(6, str.lastIndexOfChar(937,11)); System.assertEquals(0, str.lastIndexOfChar(937,5));
public Integer lastIndexOfIgnoreCase(String substring)
Type: Integer
If the substring doesn’t occur, this method returns -1.
String s1 = 'abcdaacd'; Integer i1 = s1.lastIndexOfIgnoreCase('DAAC'); System.assertEquals( 3, i1);
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.
String s1 = 'abcdaacd'; Integer i1 = s1.lastIndexOfIgnoreCase('C', 7); System.assertEquals( 6, i1);
public String left(Integer length)
Type: String
If length is greater than the String size, the entire String is returned.
String s1 = 'abcdaacd'; String s2 = s1.left(3); System.assertEquals( 'abc', s2);
public String leftPad(Integer length)
If length is less than or equal to the current String size, the entire String is returned without space padding.
Type: String
String s1 = 'abc'; String s2 = s1.leftPad(5); System.assertEquals( ' abc', s2);
public Integer length()
Type: Integer
String myString = 'abcd'; Integer result = myString.length(); System.assertEquals(result, 4);
public String mid(Integer startIndex, Integer length)
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.
String s = 'abcde'; String s2 = s.mid(2, 3); System.assertEquals( 'cde', s2);
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).
String s1 = 'Salesforce \t force.com'; String s2 = s1.normalizeSpace(); System.assertEquals( 'Salesforce force.com', s2);
public Integer offsetByCodePoints(Integer index, Integer codePointOffset)
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 threee 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.
String str = 'A \uD835\uDD0A BC'; System.assertEquals(4, str.offsetByCodePoints(0,3));
public String remove(String substring)
Type: String
String s1 = 'Salesforce and force.com'; String s2 = s1.remove('force'); System.assertEquals( 'Sales and .com', s2);
public String removeEnd(String substring)
Type: String
String s1 = 'Salesforce and force.com'; String s2 = s1.removeEnd('.com'); System.assertEquals( 'Salesforce and force', s2);
public String removeEndIgnoreCase(String substring)
Type: String
String s1 = 'Salesforce and force.com'; String s2 = s1.removeEndIgnoreCase('.COM'); System.assertEquals( 'Salesforce and force', s2);
public String removeStart(String substring)
Type: String
String s1 = 'Salesforce and force.com'; String s2 = s1.removeStart('Sales'); System.assertEquals( 'force and force.com', s2);
public String removeStartIgnoreCase(String substring)
Type: String
String s1 = 'Salesforce and force.com'; String s2 = s1.removeStartIgnoreCase('SALES'); System.assertEquals( 'force and force.com', s2);
public String repeat(String separator, Integer numberOfTimes)
Type: String
String s1 = 'SFDC'; String s2 = s1.repeat('-', 2); System.assertEquals( 'SFDC-SFDC', s2);
public String replace(String target, String replacement)
Type: String
String s1 = 'abcdbca'; String target = 'bc'; String replacement = 'xy'; String s2 = s1.replace(target, replacement); System.assertEquals('axydxya', s2);
public String replaceAll(String regExp, String replacement)
Type: String
See the Java Pattern class for information on regular expressions.
String s1 = 'a b c 5 xyz'; String regExp = '[a-zA-Z]'; String replacement = '1'; String s2 = s1.replaceAll(regExp, replacement); System.assertEquals('1 1 1 5 111', s2);
public String replaceFirst(String regExp, String replacement)
Type: String
See the Java Pattern class for information on regular expressions.
String s1 = 'a b c 11 xyz'; String regExp = '[a-zA-Z]{2}'; String replacement = '2'; String s2 = s1.replaceFirst(regExp, replacement); System.assertEquals('a b c 11 2z', s2);
public String reverse()
Type: String
public String right(Integer length)
Type: String
String s1 = 'Hello Max'; String s2 = s1.right(3); System.assertEquals( 'Max', s2);
public String rightPad(Integer length)
Type: String
String s1 = 'abc'; String s2 = s1.rightPad(5); System.assertEquals( 'abc ', s2);
public String[] split(String regExp)
Type: String[]
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.
public String splitPath(String filename) { if (filename == null) return null; List<String> parts = filename.split('\\\\'); filename = parts[parts.size()-1]; return filename; } // For example, if the file path is e:\\processed\\PPDSF100111.csv // This method splits the path and returns the last part. // Returned filename is PPDSF100111.csv
public String[] split(String regExp, Integer limit)
Type: String[]
public List<String> splitByCharacterType()
For more information about the character types used, see java.lang.Character.getType(char).
String s1 = 'Force.com platform'; List<String> ls = s1.splitByCharacterType(); System.debug(ls); // Writes this output: // (F, orce, ., com, , platform)
public List<String> splitByCharacterTypeCamelCase()
For more information about the character types used, see java.lang.Character.getType(char).
String s1 = 'Force.com platform'; List<String> ls = s1.splitByCharacterTypeCamelCase(); System.debug(ls); // Writes this output: // (Force, ., com, , platform)
public Boolean startsWithIgnoreCase(String prefix)
Type: Boolean
String s1 = 'AE86 vs EK9'; System.assert(s1.startsWithIgnoreCase('ae86'));
public String stripHtmlTags(String htmlInput)
Type: String
String s1 = '<b>hello world</b>'; String s2 = s1.stripHtmlTags(); System.assertEquals( 'hello world', s2);
public String substring(Integer startIndex)
Type: String
String s1 = 'hamburger'; System.assertEquals('burger', s1.substring(3));
public String substring(Integer startIndex, Integer endIndex)
Type: String
'hamburger'.substring(4, 8); // Returns "urge" 'smiles'.substring(1, 5); // Returns "mile"
public String substringAfter(String separator)
Type: String
String s1 = 'Force.com.platform'; String s2 = s1.substringAfter('.'); System.assertEquals( 'com.platform', s2);
public String substringAfterLast(String separator)
Type: String
String s1 = 'Force.com.platform'; String s2 = s1.substringAfterLast('.'); System.assertEquals( 'platform', s2);
public String substringBefore(String separator)
Type: String
String s1 = 'Force.com.platform'; String s2 = s1.substringBefore('.'); System.assertEquals( 'Force', s2);
public String substringBeforeLast(String separator)
Type: String
String s1 = 'Force.com.platform'; String s2 = s1.substringBeforeLast('.'); System.assertEquals( 'Force.com', s2);
public String substringBetween(String tag)
Type: String
String s1 = 'tagYellowtag'; String s2 = s1.substringBetween('tag'); System.assertEquals('Yellow', s2);
public String substringBetween(String open, String close)
Type: String
String s1 = 'xYellowy'; String s2 = s1.substringBetween('x','y'); System.assertEquals( 'Yellow', s2);
public String swapCase()
Type: String
Upper case and title case converts to lower case, and lower case converts to upper case.
String s1 = 'Force.com'; String s2 = s1.swapCase(); System.assertEquals('fORCE.COM', s2);
public String toLowerCase()
Type: String
String s1 = 'ThIs iS hArD tO rEaD'; System.assertEquals('this is hard to read', s1.toLowerCase());
public String toLowerCase(String locale)
Type: String
// Example in Turkish // An uppercase dotted "i", \u0304, which is İ // Note this contains both a İ as well as a I String s1 = 'KIYMETLİ'; String s1Lower = s1.toLowerCase('tr'); // Dotless lowercase "i", \u0131, which is ı // Note this has both a i and ı String expected = 'kıymetli'; System.assertEquals(expected, s1Lower); // Note if this was done in toLowerCase(‘en’), it would output ‘kiymetli’
public String toUpperCase()
Type: String
String myString1 = 'abcd'; String myString2 = 'ABCD'; myString1 = myString1.toUpperCase(); Boolean result = myString1.equals(myString2); System.assertEquals(result, true);
public String toUpperCase(String locale)
Type: String
// Example in Turkish // Dotless lowercase "i", \u0131, which is ı // Note this has both a i and ı String s1 = 'imkansız'; String s1Upper = s1.toUpperCase('tr'); // An uppercase dotted "i", \u0304, which is İ // Note this contains both a İ as well as a I String expected = 'İMKANSIZ'; System.assertEquals(expected, s1Upper);
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.
String s1 = ' Hello! '; String trimmed = s1.trim(); system.assertEquals('Hello!', trimmed);
public String uncapitalize()
Type: String
String s1 = 'Hello max'; String s2 = s1.uncapitalize(); System.assertEquals( 'hello max', s2);
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.
String s1 = '"Max1, ""Max2"""'; String s2 = s1.unescapeCsv(); System.assertEquals( 'Max1, "Max2"', s2);
public String unescapeEcmaScript()
Type: String
String s1 = '\"3.8\",\"3.9\"'; String s2 = s1.unescapeEcmaScript(); System.assertEquals( '"3.8","3.9"', s2);
public String unescapeHtml3()
Type: String
String s1 = '"<Black&White>"'; String s2 = s1.unescapeHtml3(); System.assertEquals( '"<Black&White>"', s2);
public String unescapeHtml4()
Type: String
If an entity isn’t recognized, it is kept as is in the returned string.
String s1 = '"<Black&White>"'; String s2 = s1.unescapeHtml4(); System.assertEquals( '"<Black&White>"', s2);
public String unescapeJava()
String s = 'Company: \\"Salesforce.com\\"'; String unescapedStr = s.unescapeJava(); System.assertEquals('Company: "Salesforce.com"', unescapedStr);
public String unescapeUnicode()
String s = 'De onde voc\u00EA \u00E9?'; String unescapedStr = s.unescapeUnicode(); System.assertEquals('De onde você é?', unescapedStr);
public String unescapeXml()
Type: String
Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.
String s1 = '"<Black&White>"'; String s2 = s1.unescapeXml(); System.assertEquals( '"<Black&White>"', s2);
public static String valueOf(Datetime datetimeToConvert)
Type: String
DateTime dt = datetime.newInstance(1996, 6, 23); String sDateTime = String.valueOf(dt); System.assertEquals('1996-06-23 00:00:00', sDateTime);
public static String valueOf(Decimal decimalToConvert)
Type: String
Decimal dec = 3.14159265; String sDecimal = String.valueOf(dec); System.assertEquals('3.14159265', sDecimal);
public static String valueOf(Double doubleToConvert)
Type: String
Double myDouble = 12.34; String myString = String.valueOf(myDouble); System.assertEquals( '12.34', myString);
public static String valueOf(Integer integerToConvert)
Type: String
Integer myInteger = 22; String sInteger = String.valueOf(myInteger); System.assertEquals('22', sInteger);
public static String valueOf(Object toConvert)
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.
List<Integer> ls = new List<Integer>(); ls.add(10); ls.add(20); String strList = String.valueOf(ls); System.assertEquals( '(10, 20)', strList);
public static String valueOfGmt(Datetime datetimeToConvert)
Type: String
// For a PST timezone: DateTime dt = datetime.newInstance(2001, 9, 14); String sDateTime = String.valueOfGmt(dt); System.assertEquals('2001-09-14 07:00:00', sDateTime);