Given a string, a total length, and a padding character, pad the left side of the string with the padding character until the string reaches the specified total length. If the string is already equal to or longer than the total length, return it unchanged. Return null if the input string is null.
Examples:
Input: str = '5', totalLength = 3, padChar = '0'
Output: '005'
Explanation: Add two '0' characters to the left to reach length 3
Input: str = 'abc', totalLength = 5, padChar = '-'
Output: '--abc'
Explanation: Add two '-' characters to the left to reach length 5
Input: str = 'hello', totalLength = 3, padChar = '*'
Output: 'hello'
Explanation: String is already longer than 3, return unchanged
Input: str = '', totalLength = 3, padChar = '0'
Output: '000'
Explanation: Empty string is padded to reach length 3
Input: str = null, totalLength = 3, padChar = '0'
Output: null
Explanation: Null input returns null
Apex Code Editor
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
Contest Alert
🏆 #CodeEveryDay October 2026 (Oct 4–15)Sponsored by FLXBL
Contest runs October 4 - 15. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 12 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.