Call: IN +91 7013283324 | +91 9347854179
(Click on WhatsApp Chat button)
Call: US +1 (415) 529-7271

Salesforce Apex Tutorial – Chapter 6: Apex Strings

Home  >>  Salesforce Apex Tutorial  >>  Salesforce Apex Tutorial – Chapter 6: Apex Strings

Salesforce Apex Tutorial – Chapter 6: Apex Strings

On October 6, 2021, Posted by , In Salesforce Apex Tutorial, With Comments Off on Salesforce Apex Tutorial – Chapter 6: Apex Strings
Salesforce Apex Tutorial – Chapter 6: Apex Strings
Salesforce Apex Tutorial – Chapter 6: Apex Strings

In Apex, a string can contain any number of characters with no character limit, as in any other programming language.

You can read the previous article Salesforce Apex Tutorial – Chapter 5: Apex Variables and next one Salesforce Apex Tutorial – Chapter 7: Apex Arrays.

For example,  String name = ‘test string name’;

In salesforce, string class provides various in-built methods.

Here’s a list of commonly used string methods:

  • Contains(substring): This method returns true if the given string contains the specified substring.
String str=’Salesforce’;String str1= ‘force’;Boolean flag = str.contains(str1);System.debug(‘flag::’,+flag); // returns true
  • abbreviate (maxWidth): This function returns an abbreviated string of the specified length. It will return the original string if the current string exceeds the specified length.
String s = ‘Hello Maximillian’;String s2 = s.abbreviate(8); System.debug(‘s2::::’+s2); // returns Hello…
  • capitalize(): This function changes the first letter to the title case of the string and returns the current String.
String s = ‘hello maximilian’;String s2 = s.capitalize();System.assertEquals(‘Hello maximillian’, s2);
  • equals(stringOrId):  This function returns true if the passed-in object contains the same binary sequence of characters as the current string and is not null. This method is used to compare a string to an object that represents a string or ID. 

Below example is used to compare a string with an object containing a string

Object obj1 = ‘abc’;String str = ‘abc’;Boolean result1 = str.equals(obj1);System.assertEquals(true, result1);
  • escapeSingleQuotes(stringToEscape): This function returns a String with an escape character (/) added before any single quotation marks. This method is helpful for creating dynamic SOQL queries, which prevent SOQL injections.
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);
  • remove(substring): This function removes all occurrences of the specified substring and returns the String result. 
String s1 = ‘Salesforce and force.com’;String s2 = s1.remove(‘force’); // returns ‘Sales and .com’System.assertEquals(‘Sales and .com’, s2);
  • reverse(): This function returns a string with all the characters reversed.
String s =’salesforce’;
String s2 = s.reverse();
System.debug(‘s2::::’+s2);//returns ecrofselas 
  • trim(): This function removes all the leading white spaces from a string and returns it. 
String s1 = ‘   Hello!   ‘;String trimmed = s1.trim();system.assertEquals(‘Hello!’, trimmed);
Comments are closed.