String companyName = 'Abc International'; System.debug('Value companyName variable'+companyName);
Here is an example of a string. It is printed into the debug log, concatenated with another string.
There is a function called contains which is used to check whether there is a substring within a string. It’s similar to the “find” function in browsers or word processors. Here is the syntax:
public Boolean contains(String substring)
The following code block declares two strings. Then it seems to declare a boolean variable “result” (or maybe this is a keyword) and checks whether the second string contained the first string. So it should return true or false based on the evaluation of that boolean statement ( I think). Then it prints out a string followed by this true/false result.
String myProductName1 = 'HCL'; String myProductName2 = 'NAHCL'; Boolean result = myProductName2.contains(myProductName1); System.debug('O/p will be true as it contains the String and Output is:'+result);
Another function is equals. It checks whether two strings are the same using the following syntax:
public Boolean equals(Object string)
It is case sensitive. Similar to the code block earlier, this one uses equals function instead of contains.
String myString1 = 'MySTRING'; String myString2 = 'MyString'; Boolean result = myString2.equalsIgnoreCase(myString1); System.debug('Value of Result will be true as they are same and Result is:'+result);
There is a case-insensitive version of the equals function; equalsIgnoreCase:
public Boolean equalsIgnoreCase(String stringtoCompare)
So this would return true:
String myString1 = 'MySTRING'; String myString2 = 'MyString'; Boolean result = myString2.equalsIgnoreCase(myString1);
There is a function called remove which removes specified strings from a string:
public String remove(String stringToRemove)
So this would remove “MyString” from “This Is MyString Example”.
String myString1 = 'This Is MyString Example'; String stringToRemove = 'MyString'; String result = myString1.remove(stringToRemove); System.debug('Value of Result will be 'This Is Example' as we have removed the MyString and Result is :'+result);
*I tested this and it returned an error. I figured it out and it is because the string it printed out wasn’t working because there was a line break.