Introduction
Salesforce provides new string methods to determine what type of characters contains in a particular string by using some of the below methods.
Methods to Determine Character Types
Alpha
- String str = 'AlphaOnly';
- System.assertEquals(true, str.isAlpha());
The above string method will return true, if the string contain only characters.
isAlphaSpace
- String str = 'AlphaOnly Space';
- System.assertEquals(true, str.isAlphaSpace());
The above string method will return true, if the string contain alphabet and white spaces.
isAlphaNumeric
- String str = 'AlphaNumeric123';
- System.assertEquals(true, str.isAlphaNumeric());
The above string method will return true, if the string contain alphabet, numbers.
isAlphanumericSpace
- String str = 'Alpha Numeric123';
- System.assertEquals(true, str.isAlphanumericSpace());
The above string method will return true, if the string contain alphabet, numbers and white spaces.
isNumeric
- String str = '432';
- System.assertEquals(true, str.isNumeric());
The above string method will return true, if the string contain numbers only.
isNumericSpace
- String str = '432 ';
- System.assertEquals(true, str.isNumericSpace());
The above string method will return true, if the string contain numbers with spaces.
isWhitespace
- String str = ' ';
- System.assertEquals(true, str.isWhitespace());
The above string method will return true, if the string contain only white spaces.