| by Arround The Web | No comments

Salesforce Apex – String Class

The Salesforce Apex string class has many in-built methods that are applied on string/text data types like industry and rating fields on the standard account object. Using these methods, we can manipulate the Salesforce data as desired. In Apex, the string can be declared using the “String” datatype. In this guide, we will focus on applying the string methods on Salesforce standard objects like accounts and contacts.

Apex String Class

String class supports all string methods which are primitive. It uses the system namespace. The datatype that is used to declare a string is a string followed by a variable. We can assign a string to this variable.

Syntax:

String variable = ”string”;

Let’s discuss the methods that are available in the Apex “string” class.

1. toLowerCase()

Basically, this method converts all characters that are present in the string to lowercase. When you need to convert the Salesforce object records (string related fields) to lowercase, you can use the toLowerCase() method.  While working with objects, you need to pass the fieldName when you are working on Salesforce objects.

Syntax:

  1. string.toLowerCase()
  2. Salesforce_obj.fieldName.toLowerCase()

Generic Example:

Let’s have a “LINUXHINT” string and convert it into lowercase.

String my_stri = 'LINUXHINT';

system.debug('Actual: '+ my_stri);

system.debug('Lowercase: '+ my_stri.toLowerCase());

Output:

With Salesforce Contact Object:

Create a “contact” object with “Department” and “Title” and apply this method on the “contact” object records.

// Create contact object with two instance records

Contact obj = new Contact(Department='SALES',Title='Manager-executive');

system.debug('Contact Data: '+obj);

// toLowerCase()

system.debug('Department in lowercase: '+obj.Department.toLowerCase());

system.debug('Title in lowercase: '+obj.Title.toLowerCase());

Output:

2. toUpperCase()

This method converts all characters that are present in the string (declared from Apex “string” class) to uppercase.

When you need to convert the Salesforce object records (string related fields) to uppercase, you can use the toUpperCase() method.  While working with objects, you need to pass the fieldName when you are working on Salesforce objects.

Syntax:

  1. string.toUpperCase()
  2. Salesforce_obj.fieldName.toUpperCase()

Generic Example:

Let’s have a “linuxhint” string and convert it into uppercase.

String my_stri = 'linuxhint';

system.debug('Actual: '+ my_stri);

system.debug('Uppercase: '+ my_stri.toUpperCase());

Output:

With Salesforce Contact Object:

Create a “contact” object with “Department” and “Title” and apply this method on the “contact” object records.

Contact obj = new Contact(Department='SALES',Title='Manager-executive');

system.debug('Contact Data: '+obj);

// toUpperCase()

system.debug('Department in uppercase: '+obj.Department.toUpperCase());

system.debug('Title in uppercase: '+obj.Title.toUpperCase());

Output:

3. capitalize()

It is good to see the first character in capitalized format. Only the first letter is capitalized by this method. Like the previous methods, it won’t take any parameters.

Syntax:

  1. string.capitalize()
  2. Salesforce_obj.fieldName.capitalize()

Generic Example:

Let’s have a “linux hint” string and convert the first character to uppercase.

String my_stri = 'linux hint';

system.debug('Actual: '+ my_stri);

system.debug(my_stri.capitalize());

Output:

With Salesforce Contact Object:

Let’s apply this method on the “contact” object fields (Department and Title) to make their first character in the string a capital.

Contact obj = new Contact(Department='sALES',Title='manager-executive');

system.debug('Contact Data: '+obj);

// capitalize()

system.debug(obj.Department.capitalize());

system.debug(obj.Title.capitalize());

Output:

4. Contains()

It is possible to check whether the string exists in another string using the Apex string contains() method. It returns a Boolean value of true if the specified string exists in the actual string. Otherwise, false is returned.

Syntax:

  1. actual_string.contains(check_string)
  2. Salesforce_obj.fieldName.contains(check_string)

Generic Example:

Let’s have a “linux hint” string and check whether the “linux” and “python” strings exist in this string or not.

String my_stri = 'linux hint';

system.debug('Actual: '+ my_stri);

system.debug('linux exists: '+my_stri.contains('linux'));

system.debug('python exists: '+my_stri.contains('python'));

Output:

With Salesforce Contact Object:

Check if the “Sales” and “Process” strings exist in the “Sales-executive” title or not.

Contact obj = new Contact(Title='Sales-executive');

system.debug('Contact Data: '+obj);

// contains()

system.debug(obj.Title.contains('Sales'));

system.debug(obj.Title.contains('Process'));

Output:

5. startsWith()

The startsWith() method in Apex “string” class returns true if the specified string starts with the given string/field value of the Salesforce object. Otherwise, false is returned. It takes a string as a parameter.

Syntax:

  1. actual_string.startsWith(check_string)
  2. Salesforce_obj.fieldName.startsWith(check_string)

Generic Example:

Let’s have a “linux hint” string and check whether it starts with the “linux” and “python” strings.

String my_stri = 'linux hint';

system.debug('Actual: '+ my_stri);

system.debug('Starts with linux: '+my_stri.startsWith('linux'));

system.debug('Starts with python: '+my_stri.startsWith('python'));

Output:

With Salesforce Contact Object:

Check if the “Sales-executive” title starts with “Sales” and “executive” separately.

Contact obj = new Contact(Title='Sales-executive');

system.debug('Contact Data: '+obj);

// startsWith()

system.debug(obj.Title.startsWith('Sales'));

system.debug(obj.Title.startsWith('executive'));

Output:

6. endsWith()

The endsWith() method in the Apex “string” class returns true if the specified string ends with the given string/field value of the Salesforce object. Otherwise, false is returned. It takes a string as a parameter.

Syntax:

  1. actual_string.endsWith(check_string)
  2. Salesforce_obj.fieldName.endsWith(check_string)

Generic Example:

Let’s have a “linux hint” string and check whether it starts with the “hint” and “linux” strings.

String my_stri = 'linux hint';

system.debug('Actual: '+ my_stri);

system.debug('Ends with hint: '+my_stri.endsWith('hint'));

system.debug('Ends with linux: '+my_stri.endsWith('linux'));

Output:

With Salesforce Contact Object:

Check if the “Sales-executive” title ends with “Sales” and “executive” separately.

Contact obj = new Contact(Title='Sales-executive');

system.debug('Contact Data: '+obj);

// endsWith()

system.debug(obj.Title.endsWith('Sales'));

system.debug(obj.Title.endsWith('executive'));

Output:

7. swapCase()

This method is available in the Apex “string” class which swaps the characters in the (Lower – Upper)/(Upper – Lower) string and return the updated string. No arguments are required to this method.

Syntax:

  1. string.swapCase()
  1. Salesforce_obj.fieldName.swapCase()

Generic Example:

Let’s have a “Linux Hint” string and swap all the characters in it.

String my_stri = 'Linux Hint';

system.debug('Actual: '+ my_stri);

system.debug('Swapped characters: '+ my_stri.swapCase());

Output:

With Salesforce Account Object:

Consider the account with the “Linux Hint” name and swap all the characters in it.

Account obj = new Account(Name='Linux Hint');

system.debug('Account Name: '+obj);

// swapCase()

system.debug(obj.Name.swapCase());

Output:

8. isAllLowerCase()

You can use this method if you want to check whether all the characters in the string are in lower case. If all the characters are in lowercase, true is returned. Otherwise, false is returned. No parameters are required to this method.

Syntax:

  1. string.isAllLowerCase()
  2. Salesforce_obj.fieldName.isAllLowerCase()

Generic Example:

Let’s have a “linuxhint” string and apply the isAllLowerCase() method to check if all the characters in the string are in lowercase.

String my_stri = 'linuxhint';

system.debug('Actual: '+ my_stri);

system.debug( my_stri.isAllLowerCase());

Output:

With Salesforce Account Object:

Check if all the characters in the “linuxhint” account name are in lowercase or not.

Account obj = new Account(Name='linuxhint');

system.debug('Account Name: '+obj);

// isAllLowerCase()

system.debug(obj.Name.isAllLowerCase());

Output:

9. isAllUpperCase()

Similar to the previous method, we can also check if all the characters in the string are in uppercase or not. It also takes no parameters and returns a Boolean value (true/false).

Syntax:

  1. string.isAllUpperCase()
  2. Salesforce_obj.fieldName.isAllUpperCase()

Generic Example:

Let’s have a “LINUXHINT” string and apply the isAllUpperCase() method to check if all the characters in the string are in lowercase.

String my_stri = 'LINUXHINT';

system.debug('Actual: '+ my_stri);

system.debug( my_stri.isAllUpperCase());

Output:

With Salesforce Account Object:

Check if all the characters in the “AGRICULTURE” account name are in uppercase or not.

Account obj = new Account(Name='AGRICULTURE');

system.debug('Account Name: '+obj);

// isAllLUpperCase()

system.debug(obj.Name.isAllUpperCase());

Output:

10. reverse()

The reverse() method in Apex “string” class reverses the given string. It also takes no parameters and returns the string.

Syntax:

  1. string.reverse()
  2. Salesforce_obj.fieldName.reverse()

Generic Example:

Let’s have a “linux hint” string and reverse it.

String my_stri = 'linuxhint';

system.debug('Actual: '+ my_stri);

system.debug('Reversed: '+ my_stri.reverse());

Output:

With Salesforce Account Object:

Create an account object with the “linuxhint” name and reverse it.

Account obj = new Account(Name='linuxhint');

system.debug('Account Name: '+obj);

// reverse()

system.debug(obj.Name.reverse());

Output:

Conclusion

We discussed the Salesforce Apex “string” class. Then, we proceed to its methods and discussed it one by one in detail. In each method, we learned how to apply these methods on simple strings and Salesforce standard objects like “Account” and “Contact”. The top 10 and useful methods in Apex “string” class are discussed along with examples and good output screenshots. After reading this article, you now know how to apply these string methods on Salesforce data.

Share Button

Source: linuxhint.com

Leave a Reply