Salesforce String Data Type

String Data Type

Anything inside a Single Quotes ( ‘ ‘ ) is known as String.

Index of String:

Salesforce String Data Type

The first character of the string stores at 0th index, second character stores at 1st index, and so on.

Example:

String str='hello world';
integer last=str.indexOf('o');
System.debug('index of o '+last);
System.debug('index of r '+str.indexOf('r'));

Output:

Salesforce String Data Type

Example:  

String not found
String str='hello world';
integer last=str.indexOf('o');
System.debug('index of x '+str.indexOf('x'));

Output:

Salesforce String Data Type

Example:  

String str='hello world';
String str2=str.capitalize();  //Hello world
System.debug('str2 = '+str2);

Output:

Salesforce String Data Type

In the above example, the capitalize method capitalizes the first character of the string.

Example:  Comparing three values

String str1='hello';
String str2='world';
Integer result=str1.compareTo(str2);
System.debug('result + '+result);  //negative value
Integer result2=str2.compareTo(str1);
System.debug('result2 '+result2);  //positive value
String str3='salesforce';
Integer result3=str2.compareTo(str3);
System.debug('result3 = '+result3);  //positive value

Output:

Salesforce String Data Type

Example:

String str1='helloworld';
String str2='hello';
Boolean result1=str1.equals(str2);
System.debug('is str1==str2 ? '+result1); //false

Output:

Salesforce String Data Type

Example:

String str1='helloworld';
String str2='heLloWorLd';
Boolean result1=str1.equals(str2);  //equals method looks for the case
System.debug('is str1==str2 ? '+result1);
Boolean result2=str1.equalsIgnoreCase(str2);
System.debug('is str2==str1 ignore case '+result2);

Output:

Salesforce String Data Type

In the above example, the 1st result returns false because the case (str1 and str2) doesn`t match, and the 2nd result returns true because both the cases are same.

Example: String in Uppercase

String str1='salesforce';
System.debug('String in uppercase ='+str1.toUpperCase());  //SALESFORCE

Output:

Salesforce String Data Type

In the above example, the toUpperCase is used to return a String in uppercase.

Example: String in Lowercase

String str1='HELLO SALESFORCE';
System.debug('String in lowercase ='+str1.toLowerCase());   //hello salesforce

Output:

Salesforce String Data Type

In the above example, the toLowerCase is used to return a String in lowercase.

Example: Concatenating two strings

String word1='Hello';
String word2='Salesforce';
System.debug('Concatenating two strings= '+ (word1+word2));  //HelloSalesforce

Output:

Salesforce String Data Type

Example:

String word1='Hello';
String word2='Salesforce';
String final=word1+word2;
System.debug('final = '+final);  //HelloSalesforce

Output:

Salesforce String Data Type

In the above example, we are storing the concatenation of two strings (word1 and word2) into another string (final), and then we are displaying the value (final).

Example:  Contain method

String word='Javatpoint';
System.debug('word contains '+word.contains('Java'));  //It will return true

Output:

Salesforce String Data Type

Example: Contain method

String word='Javatpoint';
System.debug('word contains '+word.contains('abc'));  //It will return false

Output:

Salesforce String Data Type

Example: Contain method

String word='Javatpoint';
System.debug('word contains '+word.contains('abc'));  //false
String word2='hello';
Boolean result1= word.contains(word2);
String word3='Java';
Boolean result2=word.contains(word3);
System.debug('word.contains(word2) = '+result1);  //false
System.debug('word.contains(word3) = '+result2);  //true

Output:

Salesforce String Data Type

In the above example, contains method checks if a String contains the given character is present in the string or not. If yes, then it will return true else false.