Usage of String Class 'Lowercase' and 'Uppercase' etc. in Apex #inSalesforce

Usage of String Class 'Lowercase' and 'Uppercase' etc. in Apex #inSalesforce


1. Lowercase


toLowerCase(): This converts all of the characters in the String to lowercase using the rules of the default (English US) locale.

e.g.,

String s1 = 'ThIs iS hArD tO rEaD';

System.assertEquals('this is hard to read',

s1.toLowerCase());

   

toLowerCase(locale): This converts all, of the characters in the String to lowercase using the rules of the specified locale.

e.g.

String s1 = 'KIYMETLİ';

String s1Lower = s1.toLowerCase('tr');

String expected = 'kıymetli';

System.assertEquals(expected, s1Lower);


isAllLowerCase() : This returns true if all characters in the current String are lowercase; otherwise, returns false.

e.g.,

String allLower = 'abcde';

System.assert(allLower.isAllLowerCase());


splitByCharacterTypeCamelCase(): This splits the current String by character type and returns a list of contiguous character groups of the same type as complete tokens

e.g.,

String s1 = 'Lightning.platform';

List<String> ls = s1.splitByCharacterTypeCamelCase();

System.debug(ls);


uncapitalize() : This returns the current String with the first letter in lowercase.

e.g.,

String s1 = 'Hello max';

String s2 = s1.uncapitalize();

System.assertEquals('hello max',s2);



2. Uppercase

isAllUpperCase() : This returns true if all characters in the current String are uppercase; otherwise, returns false.

e.g.,

String allUpper = 'ABCDE';

System.assert(allUpper.isAllUpperCase());


swapCase(): This swaps the case of all characters and returns the resulting String by using the default (English US) locale.

e.g.,

String s1 = 'Force.com';

String s2 = s1.swapCase();

System.assertEquals('fORCE.COM', s2);


toUpperCase() : This converts all of the characters in the String to uppercase using the rules of the default (English US) locale.

e.g.,

String myString1 = 'abcd';

String myString2 = 'ABCD';

myString1 = myString1.toUpperCase();

Boolean result = myString1.equals(myString2);

System.assertEquals(result, true);


toUpperCase(locale) : This converts all of the characters in the String to the uppercase using the rules of the specified locale.

e.g.,

String s1 = 'imkansız';

String s1Upper = s1.toUpperCase('tr');

String expected = 'İMKANSIZ';

System.assertEquals(expected, s1Upper);



Reference: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm

Comments