String Class Methods

String str = ' i am a string variable ';
System.debug('Actual String: '+str);

// capitalize string
System.debug('Capitalize String: '+str.capitalize());

// contains example
System.debug('Contains ring? :'+ str.contains('ring'));

// convert to upper case
System.debug('Upper case: '+str.toUpperCase());
// convert to lower case
System.debug('Lower case: '+str.toLowerCase());

// equals
System.debug('Is equal to ring?: '+str.equals('ring'));
String str1 = 'Manish';
String str2 = 'maNish';
System.debug('str1 equals str2: '+str1.equals(str2));
System.debug('str1 equals str2 ignore case: '+str1.toLowerCase().equals(str2.toLowerCase()));

// remove
System.debug('Remove ring: '+str.remove('ring'));

// replace
System.debug('Replace ring: '+str.replace('ring', 'rong'));

// split
System.debug('Split by space: '+str.split(' '));

Last updated