Saturday, 23 September 2017

A useful String method : normalizeSpace

Arpit Sharma


Salesforce release a very good method of string which is required in string manipulation during development i.e normalizeSpace().

Functionality of normalizeSpace Method - This method normalizes the following white space characters: space, tab, new line, carriage return, and form feed.

Remove leading white space characters, trailing white space characters, and repeating white space characters from string.

Example of normalizeSpace Method -

1
2
String unnormalizeString = ' Salesforce       \t  \n   \r  \f Normalize Example ';
System.assertEquals('Salesforce Normalize Example',unnormalizeString.normalizeSpace());//return true

Explanation of normalizeSpace Example 

1. In above example unnormalizeString string variable contains space on starting, space on end, multiple spaces in between and spacial character (tab (\t), new line (\n), carriage return (\r), and form feed (\f)).

2. In second line we have applied normalizeSpace() method on unnormalizeString string to remove all spaces and special character, so over statment will successfully passed without failing assert statement.It means string is normalized completely

So this is all about string normalization method.

Let me know if you have any suggestion, doubt, question.




About the Author

Arpit Sharma / Author & Editor

Certified Salesforce Developer | Salesforce Developer at Cognizant | Blogger