Skip to content

Commit

Permalink
Update code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
walters954 committed May 9, 2024
1 parent d01f29a commit 579008d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The `length()` method is called on a string instance and returns the number of c

**Example:**
```apex
String message = "Hello, Salesforce!";
int messageLength = message.length();
String message = 'Hello, Salesforce!';
Integer messageLength = message.length();
System.debug(messageLength); // Outputs: 17
```

Expand All @@ -22,7 +22,7 @@ In this example, `length()` calculates the number of characters in the `message`
A common use case in Salesforce is to validate the length of certain fields to ensure they meet specific criteria, such as verifying the length of a Salesforce ID. Salesforce IDs can be either 15 or 18 characters long, with the 18-character version being case-insensitive and thus safer for certain operations that might alter text case.

```apex
String recordId = "0012A00000Bcdef"; // A 15-character Salesforce ID
String recordId = '0012A00000Bcdef'; // A 15-character Salesforce ID
System.debug(recordId.length()); // Outputs: 15
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down

0 comments on commit 579008d

Please sign in to comment.