List of All the String Methods used in Apex

List of All the String Methods used in Apex

On February 5, 2024, Posted by , In Salesforce Apex Tutorial, With Comments Off on List of All the String Methods used in Apex

Here is the list of all the string methods used in Salesforce Apex programming with examples.

charAt(index): Returns the character at the specified index.

String str = 'CRSINFOSOLUTIONS'; 
System.debug(str.charAt(2)); // Outputs 'S' because count starts from '0'.

codePointAt(index): Returns the Unicode code point value of the character at the specified index.

String str = 'Hello'; 
System.debug(str.codePointAt(1)); // Outputs the Unicode code point of 'e'

compareTo(anotherString): Compares two strings lexicographically.

String str1 = 'CRS'; String str2 = 'Info Solutions'; System.debug(str1.compareTo(str2)); // Outputs a negative number because 'CRS' is lexicographically less than 'Info Solutions'

compareToIgnoreCase(anotherString): Compares two strings lexicographically, ignoring case differences.

String str1 = 'CRS'; 
String str2 = 'Crs'; 
System.debug(str1.compareToIgnoreCase(str2)); // Outputs 0 because they are equal ignoring case

concat(str): Concatenates the specified string to the end of this string.

String str1 = 'Salesforce'; 
String str2 = ' Training'; 
System.debug(str1.concat(str2)); // Outputs 'Salesforce Training'

contains(seq): Returns true if and only if this string contains the specified sequence of char values.

String str = 'Hello World'; 
System.debug(str.contains('World')); // Outputs true

endsWith(suffix): Tests if this string ends with the specified suffix.

String str = 'Hello World'; 
System.debug(str.endsWith('World')); // Outputs true

equals(anotherString): Compares this string to the specified object.

String str1 = 'CRS'; 
String str2 = 'CRS'; 
System.debug(str1.equals(str2)); // Outputs true

equalsIgnoreCase(anotherString): Compares this String to another String, ignoring case considerations.

String str1 = 'Salesforce'; 
String str2 = 'salesForce'; 
System.debug(str1.equalsIgnoreCase(str2)); // Outputs true

format(formatString, args): Returns a formatted string using the specified format string and arguments.

String formatStr = 'Name: {0}, Age: {1}'; 
String formattedStr = String.format(formatStr, new List<String>{'John', '30'}); System.debug(formattedStr); // Outputs 'Name: John, Age: 30'

indexOf(ch): Returns the index within this string of the first occurrence of the specified character.

String str = 'Hello'; 
System.debug(str.indexOf('e')); // Outputs 1

isBlank(): Returns true if the specified string is white space, empty (”), or null; otherwise, returns false.

String str = ' '; 
System.debug(str.isBlank()); // Outputs true

isEmpty(): Returns true if the specified string is empty (”); otherwise, returns false.

String str = ''; 
System.debug(str.isEmpty()); // Outputs true

lastIndexOf(ch): Returns the index within this string of the last occurrence of the specified character.

String str = 'Hello'; 
System.debug(str.lastIndexOf('l')); // Outputs 3

length(): Returns the length of this string.

String str = 'Hello'; 
System.debug(str.length()); // Outputs 5

replace(oldChar, newChar): Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

String str = 'Hello'; 
System.debug(str.replace('l', 'p')); // Outputs 'Heppo'

replaceAll(regex, replacement): Replaces each substring of this string that matches the given regular expression with the given replacement.

String str = 'Hello World'; 
System.debug(str.replaceAll('l', 'p')); // Outputs 'Heppo Worpdp'

replaceFirst(regex, replacement): Replaces the first substring of this string that matches the given regular expression with the given replacement.

String str = 'Hello World'; 
System.debug(str.replaceFirst('l', 'p')); // Outputs 'Heplo World'

split(regex): Splits this string around matches of the given regular expression.

String str = 'Salesforce Course'; 
List<String> parts = str.split(' '); 
System.debug(parts); // Outputs '(Salesforce, Course)'

startsWith(prefix): Tests if this string starts with the specified prefix.

String str = 'Hello World'; 
System.debug(str.startsWith('Hello')); // Outputs true

substring(beginIndex, endIndex): Returns a new string that is a substring of this string.

String str = 'CRS Salesforce'; 
System.debug(str.substring(0, 2)); // Outputs 'CRS'

toLowerCase(): Converts all of the characters in this String to lower case.

String str = 'SALESFORCE'; 
System.debug(str.toLowerCase()); // Outputs 'salesforce'

toUpperCase(): Converts all of the characters in this String to upper case.

String str = 'hello'; 
System.debug(str.toUpperCase()); // Outputs 'HELLO'

trim(): Returns a copy of the string, with leading and trailing whitespace omitted.

String str = ' Salesforce training '; 
System.debug(str.trim()); // Outputs 'Salesforce training'

valueOf(obj): Returns the string representation of the passed object.

Strengthen your Salesforce game at CRS Info Solutions! You’re not just learning; you’re getting insider knowledge from masters with over 15 years in the field. Our Salesforce courses are stacked with real-time projects, daily notes for that interview edge, and mentorship that guides you to certification success.

Ready to transform into a Salesforce dynamo? Register for our free demo and let CRS Info Solutions be the launchpad for your career skyrocket!

Comments are closed.