Notes
String in Apex is any set of characters with no character limit.
String companyName = 'Blue Moon Hotel';
System.debug('My company name is: '+companyName);
20:55:06:003 USER_DEBUG [2]|DEBUG|My company name is: Blue Moon Hotel
String Methods
length():
Returns the number of characters contained in the String.
String myStr = 'Salesforce Developer';
Integer result = myStr.length();
System.debug(result);
DEBUG|20
toUpperCase():
Converts all of the characters in the String to uppercase using the rules of the default (English US) locale.
String myStr = 'Salesforce Developer';
String result = myStr.toUpperCase();
System.debug(result);
DEBUG|SALESFORCE DEVELOPER
toLowerCase():
Converts all of the characters in the String to lowercase using the rules of the default (English US) locale.
String myStr = 'Salesforce DEVELOPER';
String result = myStr.toLowerCase();
System.debug(result);
DEBUG|salesforce developer
capitalize():
Returns the current String with the first letter changed to title case.
String s = 'salesforce developer';
String s2 = s.capitalize();
System.debug(s2);
DEBUG|Salesforce developer
reverse():
Returns a String with all the characters reversed.
String txt1 = 'SALESFORCE';
String txt2 = txt1.reverse();
System.debug(txt2);
DEBUG| ECROFSELAS
indexof():
Returns the index of the first occurrence of the specified substring. If the substring does not occur, this method returns -1.
String myString1 = 'abcde';
System.debug(myString1.indexof('d'));
DEBUG | 3
contains:
This method will return true if the given string contains the substring mentioned.
String myStr = 'My name is Engin';
Boolean result = myStr.contains('name');
System.debug(result);
20:57:33:004 USER_DEBUG [3]|DEBUG|true
equals:
This method will return true if the given string and the string passed in the method have the same binary sequence of characters and they are not null. You can compare the SFDC record id as well using this method. This method is case-sensitive.
String myStr = 'I am learning Apex';
Boolean result = myStr.equals('I am learning Apex');
System.debug(result);
20:58:04:003 USER_DEBUG [3]|DEBUG|true
remove:
This method removes the string provided from the given string.
String myStr = 'I am learning Apex';
String result = myStr.remove('Apex');
System.debug(result);
20:58:39:004 USER_DEBUG [3]|DEBUG|I am learning
startsWith:
This method will return true if the given string starts with the prefix provided in the method.
String myStr = 'Learning Apex is easy.';
Boolean result = myStr.startsWith('Learning');
System.debug(result);
20:59:05:006 USER_DEBUG [3]|DEBUG|true
endsWith:
Returns true if the String that called the method ends with the specified suffix.
String s = 'Hello Jason';
System.assert(s.endsWith('Jason'));
20:59:45:003 USER_DEBUG [2]|DEBUG|true
trim:
Returns a copy of the string that no longer contains any leading or trailing white space characters.
String s1 = ' Salesforce ';
String trimmed = s1.trim();
system.debug(trimmed);
21:01:12:005 USER_DEBUG [3]|DEBUG|Salesforce
charAt:
Returns the value of the character at the specified index.
String str = 'A is Omega.';
System.debug(str.charAt(0));
20:42:44:002 USER_DEBUG [2]|DEBUG|65
compareTo():
compares two strings lexicographically:
String myStr1 = 'Apex';
String myStr2 = 'Apex';
System.debug(myStr1.compareTo(myStr2));
20:45:48:003 USER_DEBUG [3]|DEBUG|0
Returns 0
if both string are equal.
substring():
extracts a part of a string and returns the extracted part in a new string.
String str = 'Apple, Banana, Kiwi';
String part = str.substring(7, 13);
System.debug(part);
lastIndexOf():
Returns the index of the last occurrence of the specified substring. If the substring does not occur, this method returns -1.
String txt ='mitsubishi';
Integer lastindex = txt.lastIndexOf('i');
System.debug('The last index of e is: ' + lastindex);
equalsIgnoreCase:
This method will return true if both strings has the same sequence of characters as the given string. However, this method is not case-sensitive.
String myStr = 'I am learning Apex';
Boolean result = myStr.equalsIgnoreCase('I AM LEARNING APEX');
System.debug(result);
removeEndIgnoreCase:
This method removes the string provided from the given string but only if it occurs at the end. This method is not case-sensitive.
String myStr = 'I have been learning Apex for couple days';
String result = myStr.removeEndIgnoreCase('for couple days');
System.debug(result);
isNotEmpty:
Returns true if the specified String is not empty ('') and not null; otherwise, returns false.
String text ='Apex';
System.debug(String.isNotEmpty(text));
USER_DEBUG [3]|DEBUG|true
isEmpty:
Returns true if the specified String is empty ('') or null; otherwise, returns false.
String text ='Apex';
System.debug(String.isEmpty(text));
USER_DEBUG [3]|DEBUG|false
capitalize:
Returns the current String with the first letter changed to title case.
String s = 'i learn apex';
String s2 = s.capitalize();
System.debug(s2);
DEBUG | I learn apex