-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d01f29a
commit 579008d
Showing
4 changed files
with
21 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,15 @@ | |
**What is a String?** | ||
A String in Apex is a sequence of characters used to handle text. String variables are created by enclosing characters within double quotes (`""`). For example: | ||
```apex | ||
String greeting = "Hello, World!"; | ||
String greeting = 'Hello, World!'; | ||
``` | ||
Strings can be empty (`""`) or contain spaces (`" "`), which may appear to be empty but are not. | ||
|
||
**Understanding the String Class** | ||
The String class in Apex is a built-in system class that provides a wealth of methods to manipulate and analyze text data. These methods can be called on any string instance using the dot (`.`) operator followed by the method name: | ||
```apex | ||
String myString = "Salesforce"; | ||
Boolean containsForce = myString.contains("force"); // returns true | ||
String myString = 'Salesforce'; | ||
Boolean containsForce = myString.contains('force'); // returns true | ||
``` | ||
|
||
### Commonly Used String Methods | ||
|
@@ -40,9 +40,9 @@ In Salesforce, string manipulation becomes crucial when dealing with various fie | |
**Example Use Case: Formatting Emails** | ||
Suppose you want to ensure all email addresses stored in Salesforce are in lowercase for standardization: | ||
```apex | ||
String email = "[email protected]"; | ||
String email = '[email protected]'; | ||
String formattedEmail = email.toLowerCase(); | ||
System.debug(formattedEmail); // Outputs: "[email protected]" | ||
System.debug(formattedEmail); // Outputs: '[email protected]' | ||
``` | ||
|
||
### Conclusion | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,9 @@ The `toLowerCase()` method is invoked on a string instance and returns a new str | |
|
||
**Example:** | ||
```apex | ||
String greeting = "Hello, World!"; | ||
String greeting = 'Hello, World!'; | ||
String whisper = greeting.toLowerCase(); | ||
System.debug(whisper); // Outputs: "hello, world!" | ||
System.debug(whisper); // Outputs: 'hello, world!' | ||
``` | ||
|
||
Here, `toLowerCase()` transforms each uppercase letter in `greeting` into its lowercase form, storing the result in `whisper`. | ||
|
@@ -22,14 +22,14 @@ Here, `toLowerCase()` transforms each uppercase letter in `greeting` into its lo | |
A practical application in Salesforce could be during the processing of email addresses. Email addresses are inherently case-insensitive, and normalizing them to lowercase before storage or comparison can enhance data consistency and reliability. | ||
|
||
```apex | ||
String email = "[email protected]"; | ||
String email = '[email protected]'; | ||
String normalizedEmail = email.toLowerCase(); | ||
System.debug(normalizedEmail); // Outputs: "[email protected]" | ||
System.debug(normalizedEmail); // Outputs: '[email protected]' | ||
``` | ||
|
||
### Challenge: Using `toLowerCase()` for Email Deduplication | ||
|
||
Suppose you are tasked with cleaning up a list of email addresses in a Salesforce application. Due to various input methods and user errors, the list might contain duplicate emails with different case formats. Using `toLowerCase()`, you can standardize all email addresses to lowercase to help identify and remove duplicates. | ||
You are tasked with cleaning up a list of email addresses in a Salesforce application. Due to various input methods and user errors, the list might contain duplicate emails with different case formats. Using `toLowerCase()`, you can standardize all email addresses to lowercase to help identify and remove duplicates. | ||
|
||
**Your task:** | ||
Write a piece of Apex code that processes a list of email strings, converts each to lowercase, and then deduplicates the list. You will start with a predefined list of emails that includes potential duplicates with varying cases. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,21 +10,23 @@ The `toUpperCase()` method is called on a string instance and returns a new stri | |
|
||
**Example:** | ||
```apex | ||
String message = "Hello, world!"; | ||
String message = 'Hello, world!'; | ||
String shout = message.toUpperCase(); | ||
System.debug(shout); // Outputs: "HELLO, WORLD!" | ||
System.debug(shout); // Outputs: 'HELLO, WORLD!' | ||
``` | ||
|
||
In this example, `toUpperCase()` converts every lowercase letter in `message` to its uppercase counterpart, and the result is stored in `shout`. | ||
|
||
**Practical Use Case** | ||
|
||
Consider a scenario in Salesforce where you need to ensure that all email addresses stored in the database are in a standard format. Since email addresses are case-insensitive, converting them to uppercase before storing or comparing them can help maintain consistency. | ||
Consider a scenario in Salesforce where you are implementing a search feature that allows users to find records based on names, addresses, or other textual attributes. Due to variations in how users may enter these details (e.g., entering "smith", "Smith", "SMITH"), search results can vary widely. To ensure that searches yield consistent and comprehensive results, converting all input data and comparison fields to uppercase before performing queries can be very effective. This method ensures that the search functionality is robust and reliable, regardless of how the search terms are entered. | ||
|
||
```apex | ||
String email = "[email protected]"; | ||
String formattedEmail = email.toUpperCase(); | ||
System.debug(formattedEmail); // Outputs: "[email protected]" | ||
String userInput = 'smith'; | ||
String searchQuery = userInput.toUpperCase(); // Convert to uppercase | ||
List<Contact> results = [SELECT Id, FirstName, LastName FROM Contact WHERE LastName = :searchQuery]; | ||
System.debug('Matching Contacts: ' + results.size()); | ||
``` | ||
|
||
### Challenge: Using `toUpperCase()` | ||
|