Introduction to Strings in Apex

Strings are one of the most commonly used data types in programming. In Apex, they are used to represent any sequence of characters, like names, messages, addresses, or any textual data. Think of them as containers for text.

Key characteristics:

  • Strings hold textual information.
  • They are enclosed in single quotes (' ') in Apex. Double quotes are typically used for other purposes in Salesforce.
  • They can be empty ('') or contain many characters.

Declaring Strings

You declare a string variable using the String keyword, followed by the variable name, an equals sign (=), and the string value enclosed in single quotes.

// Declaring a string variable named 'greeting'
String greeting = 'Hello';

// Declaring another string variable
String message = 'Welcome to Apex!';

// Declaring an empty string
String emptyString = '';

// You can also declare a string variable without assigning a value initially
String companyName;
// Later assign a value
companyName = 'Salesforce';

It's important to note the difference between an empty string ('') which has a length of 0, and a null string, which means the variable doesn't point to any string value at all.

String nothing = ''; // Empty string, length is 0
String notAssigned = null; // Null string, does not refer to any value

String Concatenation

Combining strings is a frequent operation. You use the + operator to join (concatenate) two or more strings together. You can also concatenate strings with other data types (like Integers), and Apex will try to convert the other type to a string first.

String firstName = 'Ada';
String lastName = 'Lovelace';

// Concatenating with a space in between
String fullName = firstName + ' ' + lastName; // Results in 'Ada Lovelace'
System.debug(fullName); // Output: Ada Lovelace

String city = 'London';
Integer year = 1815;
String born = 'Born in ' + city + ' in ' + year; // Integer 'year' is converted to String
System.debug(born); // Output: Born in London in 1815

Getting the Length of a String

Sometimes you need to know how many characters are in a string. The length() method provides this information. Remember that spaces and punctuation count as characters.

String simple = 'Test';
Integer len = simple.length(); // len will be 4
System.debug('Length of "' + simple + '": ' + len); // Output: Length of "Test": 4

String longer = 'This is a longer string.';
Integer longerLen = longer.length(); // longerLen will be 24
System.debug('Length of "' + longer + '": ' + longerLen); // Output: Length of "This is a longer string.": 24

String empty = '';
System.debug('Length of empty string: ' + empty.length()); // Output: Length of empty string: 0

Changing Case

Converting the case of strings is straightforward with the toUpperCase() and toLowerCase() methods. This doesn't change the original string; it returns a new string with the case converted.

String mixedCase = 'Apex Programming';

// Convert to all uppercase
String upper = mixedCase.toUpperCase(); // upper is 'APEX PROGRAMMING'

// Convert to all lowercase
String lower = mixedCase.toLowerCase(); // lower is 'apex programming'

System.debug('Original: ' + mixedCase); // Output: Original: Apex Programming
System.debug('Uppercase: ' + upper);   // Output: Uppercase: APEX PROGRAMMING
System.debug('Lowercase: ' + lower);   // Output: Lowercase: apex programming

This covers the very basics of strings. In future lessons, we'll explore more powerful string methods and manipulation techniques.

Apex Code Editor
You need to connect to Salesforce to run code.

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Note

Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.