Search Here

Apex - Strings program

 1. Concatenate Strings:


You can concatenate two or more strings using the + operator.


String firstName = 'John';

String lastName = 'Doe';

String fullName = firstName + ' ' + lastName;

System.debug(fullName); // Outputs: John Doe


2. Get Length of a String:


You can find the length of a string using the length() method.


String message = 'Hello, World!';

Integer messageLength = message.length();

System.debug('Length of message: ' + messageLength); // Outputs: Length of message: 13


3. Convert to Uppercase or Lowercase:


You can convert a string to uppercase or lowercase using the toUpperCase() and toLowerCase() methods.


String text = 'This is a Sample Text';

String uppercaseText = text.toUpperCase();

String lowercaseText = text.toLowerCase();

System.debug(uppercaseText); // Outputs: THIS IS A SAMPLE TEXT

System.debug(lowercaseText); // Outputs: this is a sample text


4. Check If a String Contains Substring:


You can check if a string contains a specific substring using the contains() method.


String sentence = 'The quick brown fox';

Boolean containsFox = sentence.contains('fox');

System.debug('Contains "fox": ' + containsFox); // Outputs: Contains "fox": true


5. Split a String into Substrings:


You can split a string into substrings using the split() method.


String csvData = 'Apple, Banana, Cherry';

List<String> fruits = csvData.split(', ');

System.debug(fruits); // Outputs: (Apple, Banana, Cherry)


6. Replace Substrings:


You can replace substrings within a string using the replace() method.


String text = 'Hello, World!';

String replacedText = text.replace('Hello', 'Hi');

System.debug(replacedText); // Outputs: Hi, World!







Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.