Escape single quotes in a string to prevent SOQL injection attacks. This method should replace all single quotes with escaped single quotes to make the string safe for use in dynamic SOQL queries.
Examples
Input: userInput = "John's Company"
Output: "John''s Company"
Explanation: Single quote is escaped with double single quotes
Input: userInput = "It's a 'great' day!"
Output: "It''s a ''great'' day!"
Explanation: All single quotes are escaped
Input: userInput = "No quotes here"
Output: "No quotes here"
Explanation: No changes needed when no single quotes present
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 July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.