No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
String Class
Namespace
Usage
For more information on Strings, see Primitive Data Types.
String Methods
The following are methods for String.
abbreviate(Integer)
Signature
public String abbreviate(Integer maxWidth)
Parameters
- maxWidth
- Type: Integer
- If maxWidth is less than four, this method throws a run-time exception.
Return Value
Type: String
Example
1String s = 'Hello Maximillian';
2String s2 =
3 s.abbreviate(8);
4System.assertEquals(
5 'Hello...', s2);
6System.assertEquals(
7 8, s2.length());abbreviate(Integer, Integer)
Signature
public String abbreviate(Integer maxWidth, Integer offset)
Parameters
- 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, abbreviate won’t return a String of length greater than maxWidth.If maxWidth is too small, this method throws a run-time exception.
- offset
- Type: Integer
Return Value
Type: String
Example
1String s =
2 'Hello Maximillian';
3// Start at M
4String s2 =
5 s.abbreviate(9,6);
6System.assertEquals(
7 '...Max...', s2);
8System.assertEquals(
9 9, s2.length());capitalize()
Signature
public String capitalize()
Return Value
Type: String
Usage
This method is based on the Character.toTitleCase(char) Java method.
Example
1String s =
2 'hello maximillian';
3String s2 =
4 s.capitalize();
5System.assertEquals(
6 'Hello maximillian',
7 s2);center(Integer)
Signature
public String center(Integer size)
Parameters
- size
- Type: Integer
Return Value
Type: String
Example
1String s = 'hello';
2String s2 =
3 s.center(9);
4System.assertEquals(
5 ' hello ',
6 s2);center(Integer, String)
Signature
public String center(Integer size, String padStr)
Return Value
Type: String
Example
1String s = 'hello';
2String s2 =
3 s.center(9);
4System.assertEquals(
5 '--hello--',
6 s2);compareTo(String)
Signature
public Integer compareTo(String compString)
Parameters
- compString
- Type: String
Return Value
Type: Integer
Usage
The result is:
- A negative Integer if the String that called the method lexicographically precedes compString
- A positive Integer if the String that called the method lexicographically follows compString
- 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.
Example
1String myString1 = 'abcde';
2String myString2 = 'abcd';
3Integer result =
4 myString1.compareTo(myString2);
5System.assertEquals(result, 1);contains(String)
Signature
public Boolean contains(String compString)
Parameters
- compString
- Type: String
Return Value
Type: Boolean
Example
1String myString1 = 'abcde';
2String myString2 = 'abcd';
3Boolean result =
4 myString1.contains(myString2);
5System.assertEquals(result, true);containsAny(String)
Signature
public Boolean containsAny(String compString)
Parameters
- compString
- Type: String
Return Value
Type: Boolean
Example
1String s = 'hello';
2Boolean b1 =
3 s.containsAny('hx');
4Boolean b2 =
5 s.containsAny('x');
6System.assertEquals(
7 true,
8 b1);
9System.assertEquals(
10 false,
11 b2);containsIgnoreCase(String)
Signature
public Boolean containsIgnoreCase(String compString)
Parameters
- compString
- Type: String
Return Value
Type: Boolean
Example
1String s = 'hello';
2Boolean b =
3 s.containsIgnoreCase('HE');
4System.assertEquals(
5 true,
6 b);containsNone(String)
Signature
public Boolean containsNone(String compString)
Parameters
- compString
- Type: String
- If compString is an empty string or the current String is empty, this method returns true.If compString is null, this method returns a run-time exception.
Return Value
Type: Boolean
containsOnly(String)
Signature
public Boolean containsOnly(String compString)
Parameters
- compString
- Type: String
Return Value
Type: Boolean
Example
1String s1 = 'abba';
2String s2 = 'abba xyz';
3Boolean b1 =
4 s1.containsOnly('abcd');
5System.assertEquals(
6 true,
7 b1);
8Boolean b2 =
9 s2.containsOnly('abcd');
10System.assertEquals(
11 false,
12 b2);containsWhitespace()
Signature
public Boolean containsWhitespace()
Return Value
Type: Boolean
deleteWhitespace()
Signature
public String deleteWhitespace()
Return Value
Type: String
difference(String)
Signature
public String difference(String compString)
Parameters
- compString
- Type: String
- If compString is an empty string, this method returns an empty string.If compString is null, this method throws a run-time exception.
Return Value
Type: String
Example
1String s = 'Hello Jane';
2String d1 =
3 s.difference('Hello Max');
4System.assertEquals(
5 'Max',
6 d1);
7String d2 =
8 s.difference('Goodbye');
9System.assertEquals(
10 'Goodbye',
11 d2);equals(String)
Signature
public Boolean equals(String compString)
Parameters
- compString
- Type: String
Return Value
Type: Boolean
Usage
Note that the == operator also performs String comparison, but is case-insensitive to match Apex semantics. (== is case-sensitive for ID comparison for the same reason.)
Example
1String myString1 = 'abcde';
2String myString2 = 'abcd';
3Boolean result =
4 myString1.equals(myString2);
5System.assertEquals(result, false);equalsIgnoreCase(String)
Signature
public Boolean equalsIgnoreCase(String compString)
Parameters
- compString
- Type: String
Return Value
Type: Boolean
Example
1String myString1 = 'abcd';
2String myString2 = 'ABCD';
3Boolean result =
4myString1.equalsIgnoreCase(myString2);
5System.assertEquals(result, true);escapeCsv()
Signature
public String escapeCsv()
Return Value
Type: String
Usage
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.
Example
1String s1 =
2 'Max1, "Max2"';
3String s2 =
4 s1.escapeCsv();
5System.assertEquals(
6 '"Max1, ""Max2"""',
7 s2);escapeEcmaScript()
Signature
public String escapeEcmaScript()
Return Value
Type: String
Usage
The only difference between Apex strings and EcmaScript strings is that in EcmaScript, a single quote and forward-slash (/) are escaped.
Example
1String s1 = '"grade": 3.9/4.0';
2String s2 = s1.escapeEcmaScript();
3System.debug(s2);
4// Output is:
5// \"grade\": 3.9\/4.0
6System.assertEquals(
7 '\\"grade\\": 3.9\\/4.0',
8 s2);escapeHtml3()
Signature
public String escapeHtml3()
Return Value
Type: String
Example
1String s1 =
2 '"<Black&White>"';
3String s2 =
4 s1.escapeHtml3();
5System.debug(s2);
6// Output:
7// "<Black&
8// White>"escapeHtml4()
Signature
public String escapeHtml4()
Return Value
Type: String
Example
1String s1 =
2 '"<Black&White>"';
3String s2 =
4 s1.escapeHtml4();
5System.debug(s2);
6// Output:
7// "<Black&
8// White>"escapeJava()
Signature
public String escapeJava()
Example
1// Input string contains quotation marks
2String s = 'Company: "Salesforce.com"';
3String escapedStr = s.escapeJava();
4// Output string has the quotes escpaded
5System.assertEquals('Company: \\"Salesforce.com\\"', escapedStr);escapeSingleQuotes(String)
Signature
public static String escapeSingleQuotes(String stringToEscape)
Parameters
- stringToEscape
- Type: String
Return Value
Type: String
Usage
This method is useful when creating a dynamic SOQL statement, to help prevent SOQL injection. For more information on dynamic SOQL, see Dynamic SOQL.
escapeUnicode()
Signature
public String escapeUnicode()
Example
1String s = 'De onde você é?';
2String escapedStr = s.escapeUnicode();
3System.assertEquals('De onde voc\\u00EA \\u00E9?', escapedStr);escapeXml()
Signature
public String escapeXml()
Return Value
Type: String
Usage
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.
Example
1String s1 =
2 '"<Black&White>"';
3String s2 =
4 s1.escapeXml();
5System.debug(s2);
6// Output:
7// "<Black&
8// White>"format(String, List<String>)
Signature
public static String format(String stringToFormat, List<String> formattingArguments)
Return Value
Type: String
getCommonPrefix(List<String>)
Signature
public static String getCommonPrefix(List<String> strings)
Return Value
Type: String
Example
1List<String> ls =
2 new List<String>
3 {'SFDCApex',
4 'SFDCVisualforce'};
5String prefix =
6 String.getCommonPrefix(
7 ls);
8System.assertEquals(
9 'SFDC', prefix);getLevenshteinDistance(String)
Signature
public Integer getLevenshteinDistance(String stringToCompare)
Parameters
- stringToCompare
- Type: String
Return Value
Type: Integer
Usage
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
1String s = 'Hello Joe';
2Integer i =
3 s.getLevenshteinDistance(
4 'Hello Max');
5System.assertEquals(
6 3, i);getLevenshteinDistance(String, Integer)
Signature
public Integer getLevenshteinDistance(String stringToCompare, Integer threshold)
Return Value
Type: Integer
Usage
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.
Example
1String s = 'Hello Jane';
2Integer i =
3 s.getLevenshteinDistance(
4 'Hello Max', 2);
5System.assertEquals(
6 -1, i);hashCode()
Signature
public Integer hashCode()
Return Value
Type: Integer
Usage
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.
Example
1public class MyCustomClass {
2 String x,y;
3 // Provide a custom hash code
4 public Integer hashCode() {
5 return
6 (31*x.hashCode())^(y.hashCode());
7 }
8}indexOf(String, Integer)
Signature
public Integer indexOf(String substring, Integer index)
Return Value
Type: Integer
Example
1String myString1 = 'abcd';
2String myString2 = 'bc';
3Integer result =
4 myString1.indexOf(myString2, 0);
5System.assertEquals(1, result);indexOfAny(String)
Signature
public Integer indexOfAny(String substring)
Parameters
- substring
- Type: String
Return Value
Type: Integer
Example
1String s1 = 'abcd';
2String s2 = 'xc';
3Integer result =
4 s1.indexOfAny(s2);
5System.assertEquals(
6 2, result);indexOfAnyBut(String)
Signature
public Integer indexOfAnyBut(String substring)
Parameters
- substring
- Type: String
Return Value
Type: Integer
Example
1String s1 = 'abcd';
2String s2 = 'xc';
3Integer result =
4 s1.indexOfAnyBut(s2);
5System.assertEquals(
6 0, result);indexOfDifference(String)
Signature
public Integer indexOfDifference(String stringToCompare)
Parameters
- stringToCompare
- Type: String
Return Value
Type: Integer
Example
1String s1 = 'abcd';
2String s2 = 'abxc';
3Integer result =
4 s1.indexOfDifference(s2);
5System.assertEquals(
6 2, result);indexOfIgnoreCase(String)
Signature
public Integer indexOfIgnoreCase(String substring)
Parameters
- substring
- Type: String
Return Value
Type: Integer
Example
1String s1 = 'abcd';
2String s2 = 'BC';
3Integer result =
4 s1.indexOfIgnoreCase(s2, 0);
5System.assertEquals(1, result);indexOfIgnoreCase(String, Integer)
Signature
public Integer indexOfIgnoreCase(String substring, Integer startPosition)
Return Value
Type: Integer
isAllLowerCase()
Signature
public Boolean isAllLowerCase()
Return Value
Type: Boolean
isAllUpperCase()
Signature
public Boolean isAllUpperCase()
Return Value
Type: Boolean
isAlpha()
Signature
public Boolean isAlpha()
Return Value
Type: Boolean
Example
1// Letters only
2String s1 = 'abc';
3// Returns true
4Boolean b1 =
5 s1.isAlpha();
6System.assertEquals(
7 true, b1);
8
9// Letters and numbers
10String s2 = 'abc 21';
11// Returns false
12Boolean b2 =
13 s2.isAlpha();
14System.assertEquals(
15 false, b2);isAlphaSpace()
Signature
public Boolean isAlphaSpace()
Return Value
Type: Boolean
isAlphanumeric()
Signature
public Boolean isAlphanumeric()
Return Value
Type: Boolean
Example
1// Letters only
2String s1 = 'abc';
3// Returns true
4Boolean b1 =
5 s1.isAlphanumeric();
6System.assertEquals(
7 true, b1);
8
9// Letters and numbers
10String s2 = 'abc021';
11// Returns true
12Boolean b2 =
13 s2.isAlphanumeric();
14System.assertEquals(
15 true, b2);isAlphanumericSpace()
Signature
public Boolean isAlphanumericSpace()
Return Value
Type: Boolean
isAsciiPrintable()
Signature
public Boolean isAsciiPrintable()
Return Value
Type: Boolean
isNumeric()
Signature
public Boolean isNumeric()
Return Value
Type: Boolean
isNumericSpace()
Signature
public Boolean isNumericSpace()
Return Value
Type: Boolean
Usage
A decimal point (1.2) is not a Unicode digit.
isWhitespace()
Signature
public Boolean isWhitespace()
Return Value
Type: Boolean
join(Object, String)
Signature
public static String join(Object iterableObj, String separator)
Parameters
- iterableObj
- Type: Object
- separator
- Type: String
Return Value
Type: String
Usage
1List<Integer> li = new
2 List<Integer>
3 {10, 20, 30};
4String s = String.join(
5 li, '/');
6System.assertEquals(
7 '10/20/30', s);lastIndexOf(String, Integer)
Signature
public Integer lastIndexOf(String substring, Integer endPosition)
Return Value
Type: Integer
Usage
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.
Example
1String s1 = 'abcdaacd';
2Integer i1 =
3 s1.lastIndexOf('c', 7);
4System.assertEquals(
5 6, i1);
6Integer i2 =
7 s1.lastIndexOf('c', 3);
8System.assertEquals(
9 2, i2);lastIndexOfIgnoreCase(String)
Signature
public Integer lastIndexOfIgnoreCase(String substring)
Parameters
- substring
- Type: String
Return Value
Type: Integer
Usage
If the substring doesn’t occur, this method returns -1.
Example
1String s1 = 'abcdaacd';
2Integer i1 =
3 s1.lastIndexOfIgnoreCase('DAAC');
4System.assertEquals(
5 3, i1);lastIndexOfIgnoreCase(String, Integer)
Signature
public Integer lastIndexOfIgnoreCase(String substring, Integer endPosition)
Return Value
Type: Integer
Usage
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.
Example
1String s1 = 'abcdaacd';
2Integer i1 =
3 s1.lastIndexOfIgnoreCase('C', 7);
4System.assertEquals(
5 6, i1);left(Integer)
Signature
public String left(Integer length)
Parameters
- length
- Type: Integer
Return Value
Type: String
Usage
If length is greater than the String size, the entire String is returned.
Example
1String s1 = 'abcdaacd';
2String s2 =
3 s1.left(3);
4System.assertEquals(
5 'abc', s2);leftPad(Integer)
Signature
public String leftPad(Integer length)
Parameters
- length
- Type: Integer
Usage
If length is less than or equal to the current String size, the entire String is returned without space padding.
Return Value
Type: String
Example
1String s1 = 'abc';
2String s2 =
3 s1.leftPad(5);
4System.assertEquals(
5 ' abc', s2);length()
Signature
public Integer length()
Return Value
Type: Integer
Example
1String myString = 'abcd';
2Integer result = myString.length();
3System.assertEquals(result, 4);mid(Integer, Integer)
Signature
public String mid(Integer startIndex, Integer length)
Parameters
Return Value
Type: String
Usage
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.
Example
1String s = 'abcde';
2String s2 = s.mid(2, 3);
3System.assertEquals(
4 'cde', s2);normalizeSpace()
Signature
public String normalizeSpace()
Return Value
Type: String
Usage
This method normalizes the following white space characters: space, tab (\t), new line (\n), carriage return (\r), and form feed (\f).
Example
1String s1 =
2 'Salesforce \t force.com';
3String s2 =
4 s1.normalizeSpace();
5System.assertEquals(
6 'Salesforce force.com', s2);remove(String)
Signature
public String remove(String substring)
Parameters
- substring
- Type: String
Return Value
Type: String
Example
1String s1 = 'Salesforce and force.com';
2String s2 =
3 s1.remove('force');
4System.assertEquals(
5 'Sales and .com', s2);removeEnd(String)
Signature
public String removeEnd(String substring)
Parameters
- substring
- Type: String
Return Value
Type: String
Example
1String s1 = 'Salesforce and force.com';
2String s2 =
3 s1.removeEnd('.com');
4System.assertEquals(
5 'Salesforce and force', s2);removeEndIgnoreCase(String)
Signature
public String removeEndIgnoreCase(String substring)
Parameters
- substring
- Type: String
Return Value
Type: String
Example
1String s1 = 'Salesforce and force.com';
2String s2 =
3 s1.removeEndIgnoreCase('.COM');
4System.assertEquals(
5 'Salesforce and force', s2);removeStart(String)
Signature
public String removeStart(String substring)
Parameters
- substring
- Type: String
Return Value
Type: String
Example
1String s1 = 'Salesforce and force.com';
2String s2 =
3 s1.removeStart('Sales');
4System.assertEquals(
5 'force and force.com', s2);removeStartIgnoreCase(String)
Signature
public String removeStartIgnoreCase(String substring)
Parameters
- substring
- Type: String
Return Value
Type: String
Example
1String s1 = 'Salesforce and force.com';
2String s2 =
3 s1.removeStartIgnoreCase('SALES');
4System.assertEquals(
5 'force and force.com', s2);repeat(String, Integer)
Signature
public String repeat(String separator, Integer numTimes)
Return Value
Type: String
Example
1String s1 = 'SFDC';
2String s2 =
3 s1.repeat('-', 2);
4System.assertEquals(
5 'SFDC-SFDC', s2);replaceAll(String, String)
replaceFirst(String, String)
reverse()
Signature
public String reverse()
Return Value
Type: String
right(Integer)
Signature
public String right(Integer length)
Parameters
- length
- Type: Integer
- If length is greater than the String size, the entire String is returned.
Return Value
Type: String
Example
1String s1 = 'Hello Max';
2String s2 =
3 s1.right(3);
4System.assertEquals(
5 'Max', s2);rightPad(Integer)
Signature
public String rightPad(Integer length)
Parameters
- length
- Type: Integer
- If length is less than or equal to the current String size, the entire String is returned without space padding.
Return Value
Type: String
Example
1String s1 = 'abc';
2String s2 =
3 s1.rightPad(5);
4System.assertEquals(
5 'abc ', s2);split(String, Integer)
Signature
public String[] split(String regExp, Integer limit)
Return Value
Type: String[]
Usage
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.
- If limit is greater than zero, the pattern is applied at most limit - 1 times, the list's length is no greater than limit, and the list's last entry contains all input beyond the last matched delimiter.
- If limit is non-positive then the pattern is applied as many times as possible and the list can have any length.
- If limit is zero then the pattern is applied as many times as possible, the list can have any length, and trailing empty strings are discarded.
- s.split(':', 2) results in {'boo', 'and:foo'}
- s.split(':', 5) results in {'boo', 'and', 'foo'}
- s.split(':', -2) results in {'boo', 'and', 'foo'}
- s.split('o', 5) results in {'b', '', ':and:f', '', ''}
- s.split('o', -2) results in {'b', '', ':and:f', '', ''}
- s.split('o', 0) results in {'b', '', ':and:f'}
Example
In the following example, a string is split, using a backslash as a delimiter.
1public String splitPath(String filename) {
2 if (filename == null)
3 return null;
4 List<String> parts = filename.split('\\\\');
5 filename = parts[parts.size()-1];
6 return filename;
7}
8
9// For example, if the file path is e:\\processed\\PPDSF100111.csv
10// This method splits the path and returns the last part.
11// Returned filename is PPDSF100111.csvsplitByCharacterType()
Signature
public List<String> splitByCharacterType()
Usage
For more information about the character types used, see java.lang.Character.getType(char).
Example
1String s1 = 'Force.com platform';
2List<String> ls =
3 s1.splitByCharacterType();
4System.debug(ls);
5// Writes this output:
6// (F, orce, ., com, , platform)splitByCharacterTypeCamelCase()
Signature
public List<String> splitByCharacterTypeCamelCase()
Usage
For more information about the character types used, see java.lang.Character.getType(char).
Example
1String s1 = 'Force.com platform';
2List<String> ls =
3 s1.splitByCharacterTypeCamelCase();
4System.debug(ls);
5// Writes this output:
6// (Force, ., com, , platform)stripHtmlTags(String)
Signature
public String stripHtmlTags(String htmlInput)
Parameters
- htmlInput
- Type: String
Return Value
Type: String
Example
1String s1 = '<b>hello world</b>';
2String s2 = s1.stripHtmlTags();
3System.assertEquals(
4 'hello world', s2);substring(Integer, Integer)
Signature
public String substring(Integer startIndex, Integer endIndex)
Return Value
Type: String
Example
1'hamburger'.substring(4, 8);
2// Returns "urge"
3
4'smiles'.substring(1, 5);
5// Returns "mile"substringAfter(String)
Signature
public String substringAfter(String separator)
Parameters
- separator
- Type: String
Return Value
Type: String
Example
1String s1 = 'Force.com.platform';
2String s2 =
3 s1.substringAfter('.');
4System.assertEquals(
5 'com.platform', s2);substringAfterLast(String)
Signature
public String substringAfterLast(String separator)
Parameters
- separator
- Type: String
Return Value
Type: String
Example
1String s1 = 'Force.com.platform';
2String s2 =
3 s1.substringAfterLast('.');
4System.assertEquals(
5 'platform', s2);substringBefore(String)
Signature
public String substringBefore(String separator)
Parameters
- separator
- Type: String
Return Value
Type: String
Example
1String s1 = 'Force.com.platform';
2String s2 =
3 s1.substringBefore('.');
4System.assertEquals(
5 'Force', s2);substringBeforeLast(String)
Signature
public String substringBeforeLast(String separator)
Parameters
- separator
- Type: String
Return Value
Type: String
Example
1String s1 = 'Force.com.platform';
2String s2 =
3 s1.substringBeforeLast('.');
4System.assertEquals(
5 'Force.com', s2);substringBetween(String)
Signature
public String substringBetween(String tag)
Parameters
- tag
- Type: String
Return Value
Type: String
Example
1String s1 = 'tagYellowtag';
2String s2 =
3 s1.substringBetween('tag');
4System.assertEquals(
5 'Yellow', s2);substringBetween(String, String)
Signature
public String substringBetween(String open, String close)
Return Value
Type: String
Example
1String s1 = 'xYellowy';
2String s2 =
3 s1.substringBetween('x','y');
4System.assertEquals(
5 'Yellow', s2);swapCase(String, String)
Signature
public String swapCase(String open, String close)
Return Value
Type: String
Usage
Upper case and title case converts to lower case, and lower case converts to upper case.
Example
1String s1 = 'Force.com';
2String s2 =
3 s1.swapCase();
4System.assertEquals(
5 'fORCE.COM', s2);toLowerCase()
Signature
public String toLowerCase()
Return Value
Type: String
toUpperCase()
Signature
public String toUpperCase()
Return Value
Type: String
Example
1String myString1 = 'abcd';
2String myString2 = 'ABCD';
3myString1 =
4 myString1.toUpperCase();
5Boolean result =
6 myString1.equals(myString2);
7System.assertEquals(result, true);trim()
Signature
public String trim()
Return Value
Type: String
Usage
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.
uncapitalize()
Signature
public String uncapitalize()
Return Value
Type: String
Example
1String s1 =
2 'Hello max';
3String s2 =
4 s1.uncapitalize();
5System.assertEquals(
6 'hello max',
7 s2);unescapeCsv()
Signature
public String unescapeCsv()
Return Value
Type: String
Usage
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.
Example
1String s1 =
2 '"Max1, ""Max2"""';
3String s2 =
4 s1.unescapeCsv();
5System.assertEquals(
6 'Max1, "Max2"',
7 s2);unescapeEcmaScript()
Signature
public String unescapeEcmaScript()
Return Value
Type: String
Example
1String s1 =
2 '\"3.8\",\"3.9\"';
3String s2 =
4 s1.unescapeEcmaScript();
5System.assertEquals(
6 '"3.8","3.9"',
7 s2);unescapeHtml3()
Signature
public String unescapeHtml3()
Return Value
Type: String
Example
1String s1 =
2 '"<Black&White>"';
3String s2 =
4 s1.unescapeHtml3();
5System.assertEquals(
6 '"<Black&White>"',
7 s2);unescapeHtml4()
Signature
public String unescapeHtml4()
Return Value
Type: String
Usage
If an entity isn’t recognized, it is kept as is in the returned string.
Example
1String s1 =
2 '"<Black&White>"';
3String s2 =
4 s1.unescapeHtml4();
5System.assertEquals(
6 '"<Black&White>"',
7 s2);unescapeJava()
Signature
public String unescapeJava()
Example
1String s = 'Company: \\"Salesforce.com\\"';
2String unescapedStr = s.unescapeJava();
3System.assertEquals('Company: "Salesforce.com"', unescapedStr);unescapeUnicode()
Signature
public String unescapeUnicode()
Example
1String s = 'De onde voc\\u00EA \\u00E9?';
2String unescapedStr = s.unescapeUnicode();
3System.assertEquals('De onde você é?', unescapedStr);unescapeXml()
Signature
public String unescapeXml()
Return Value
Type: String
Usage
Supports only the five basic XML entities (gt, lt, quot, amp, apos). Does not support DTDs or external entities.
Example
1String s1 =
2 '"<Black&White>"';
3String s2 =
4 s1.unescapeXml();
5System.assertEquals(
6 '"<Black&White>"',
7 s2);valueOf(Double)
Signature
public static String valueOf(Double doubleToConvert)
Parameters
- doubleToConvert
- Type: Double
Return Value
Type: String
Example
1Double myDouble = 12.34;
2String myString =
3 String.valueOf(myDouble);
4System.assertEquals(
5 '12.34', myString);valueOf(Object)
Signature
public static String valueOf(Object toConvert)
Parameters
- toConvert
- Type: Object
Return Value
Type: String
Usage
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.
Example
1List<Integer> ls =
2 new List<Integer>();
3ls.add(10);
4ls.add(20);
5String strList =
6 String.valueOf(ls);
7System.assertEquals(
8 '(10, 20)', strList);