• Lessons Home
Topics
Community
  1. Lessons
  2. Strings and Dates in Depth
  3. String Methods
  4. Taking Substrings

      Taking Substrings

      Taking Substrings

      Often you need only a piece of a string: the prefix of a product code, the name in front of an email address, the file extension at the end. substring() cuts that piece out.

      Substring From a Position

      Give one index and you get everything from that position to the end. Positions count from zero:

      String code = 'LC-1234';
      System.debug(code.substring(3)); // 1234
      

      Substring Between Two Positions

      Two indexes give you the characters from the first index up to but NOT including the second:

      System.debug(code.substring(0, 2)); // LC
      

      Left and Right

      These are shortcuts when you count from either end:

      System.debug(code.left(2)); // LC
      System.debug(code.right(4)); // 1234
      

      Finding the Cut Point

      Combine indexOf() or lastIndexOf() with substring() to cut at a marker instead of a fixed position:

      String email = 'ada@example.com';
      Integer at = email.indexOf('@');
      System.debug(email.substring(0, at)); // ada
      System.debug(email.substring(at + 1)); // example.com
      

      lastIndexOf() works the same way but searches from the end, which is what you want when the marker can appear more than once.

      Common Mistakes

      An index past the end of the string throws a StringException, so check length() when you are not sure. And remember that indexOf() returns -1 when the marker is missing, which is never a safe index to cut at.

      Why This Matters

      Real data almost never arrives in exactly the shape you need. Slicing text apart at a known marker is the everyday tool for pulling the useful piece out of it.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      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.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In