Apex Strings & String Methods

Strings in Apex

A string in Apex is a sequence of characters used to represent text. Just like in C#, it’s widely used for storing and manipulating text data. 

You declare a string variable using the String keyword. 

Example: Creating a Simple String

apex 

String myName = ‘Mubashira’; 

System.debug(myName); // Output: Mubashira

Just like you have a name, every element in a program needs a name too. 
That’s what identifiers are — names for your variables, classes, and methods. 

Without names, you wouldn’t know how to call or use things in your program! 

String Properties and Common Methods in ApexShape

1. Get Length of a String

Returns the number of characters in the string using .length() 

apex 

 

String myName = ‘Mubashira’; 

Integer size = myName.length(); 

System.debug(size); // Output: 9 

2. Concatenation

a. Using + operator

apex 
 

String firstName = ‘Mubashira’; 

String lastName = ‘Khan’; 

System.debug(firstName + ‘ ‘ + lastName); // Output: Mubashira Khan 

b. Using String.format() (similar to placeholder format)

apex 

String firstName = ‘Mubashira’; 

String lastName = ‘Khan’; 

System.debug(String.format(‘{0} {1}’, new List<String>{firstName, lastName})); // Output: Mubashira Khan 

3. Case Conversion

apex 

 

String myName = ‘Mubashira’; 

System.debug(myName.toUpperCase()); // Output: MUBASHIRA 

System.debug(myName.toLowerCase()); // Output: mubashira 

 

4. Substring

Apex 

 

String myName = ‘Mubashira’; 

// First 3 characters 

System.debug(myName.substring(0, 3)); // Output: Mub 

// Last 3 characters 

System.debug(myName.substring(myName.length() – 3)); // Output: ira 

5. Trim Whitespace

Apex 

 

String nameWithSpaces = ‘  Mubashira Khan  ‘; 

System.debug(nameWithSpaces.trim()); // Output: Mubashira Khan 

6. Contains, StartsWith, EndsWith

apex 

 

String name = ‘Mubashira Khan’; 

 

System.debug(name.contains(‘Mub’)); // Output: true 

System.debug(name.startsWith(‘Mub’)); // Output: true 

System.debug(name.endsWith(‘Khan’)); // Output: true 

7. Equals

Apex 

 

String name1 = ‘Mubashira’; 

String name2 = ‘Mubashira’; 

 

System.debug(name1.equals(name2)); // Output: true 

8. Copy (Simply assign it to a new variable)

apex 

 

String name = ‘Mubashira’; 

String copyName = name; 

System.debug(copyName); // Output: Mubashira 

9. Replace

apex 

 

String fullName = ‘Mubashira Khan’; 

String newName = fullName.replace(‘Mubashira’, ‘Tuba’); 

String replacedChar = fullName.replace(‘a’, ‘@’); 

 

System.debug(newName);      // Output: Tuba Khan 

System.debug(replacedChar); // Output: Mub@shir@ Kh@n 

10. Insert (using substring + concatenation)

Apex doesn’t have a built-in insert method, but you can simulate it: 

Apex 

 

String name = ‘Mubashira’; 

String newName = name.substring(0, 9) + ‘ Khan’; 

System.debug(newName); // Output: Mubashira Khan 

11. Remove (using substring)

apex 

 

String fullName = ‘Mubashira Khan’; 

String shortened = fullName.substring(0, 9); // Removes after index 8 

System.debug(shortened); // Output: Mubashira 

Practice Tasks

1.Create a Simple String:

Declare myName = ‘YourName and print it

2. Length:

Print the length of myName.

3. Concatenation:

• Use + to join firstName and lastName.
• Use String.format().

4. Case Conversion:

Convert myName to uppercase and lowercase.

5. Substring:

Get first 3 characters. 
Get last 3 characters.

6. Trim:

Trim a string with leading/trailing spaces.

7. Contains / StartsWith / EndsWith:

•  Check if name containsMub‘. 
•  Starts with ‘Mu’? Ends with ‘Khan’?

8. Equals:

Compare name1 and name2.

9. Copy:

Copy myName into another variable.

10. Replace:

Replace your first name.  
Replace ‘a’ with ‘@’. 

11. Insert:

Simulate inserting middle name into full name.

12. Remove:

Remove last name from full name.