Sanitize Phone Numbers in Kotlin with Regex for Improved Consistency
Sanitize Phone Numbers in Kotlin with Regex for Improved Consistency

Efficiently Remove Unwanted Characters from Phone Numbers in Kotlin

Learn efficient Kotlin regex methods for sanitizing phone number inputs, enhancing data consistency and user experience.5 min


Handling phone number inputs in Kotlin can often get messy, especially when users throw in extra characters, spaces, dashes, parentheses, and more. You’ve probably encountered this scenario: a user submits their phone number as “(123)-456 7890”, “+1-800-555-0199”, or worse, “++(001)-23 456!”. Trying to process these unpredictable formats without thoroughly sanitizing the input can cause errors or unexpected behavior in your app.

Sanitizing phone numbers efficiently in Kotlin isn’t just about aesthetics—it’s crucial for backend integrations, data consistency, and providing a seamless user experience. In this article, we’ll explore the current methods commonly used to clean phone numbers, discuss their limitations, and introduce a more efficient solution with practical Kotlin implementation that you can use right away.

Current Approach to Phone Number Sanitization in Kotlin

A common approach developers take is using Kotlin’s built-in string APIs to filter unwanted characters. For instance, you might be currently using a function similar to this:


fun sanitizePhoneNumber(phone: String): String {
    return phone.filter { it.isDigit() || it == '+' }
}

At first glance, this approach looks simple and practical. But does it always work as expected? Let’s test it with some challenging examples:


fun main() {
    val inputs = listOf("(123)-456 7890", "+1-800-555-0199", "++(001)-23 456!", " 12+3 456--7890 ")

    inputs.forEach { number ->
        println("Original: '$number' ==> Sanitized: '${sanitizePhoneNumber(number)}'")
    }
}

The results might surprise you:


Original: '(123)-456 7890' ==> Sanitized: '1234567890'
Original: '+1-800-555-0199' ==> Sanitized: '+18005550199'
Original: '++(001)-23 456!' ==> Sanitized: '++00123456'
Original: ' 12+3 456--7890 ' ==> Sanitized: '12+34567890'

Spot the issue? The function retains multiple ‘+’ signs and allows ‘+’ characters to appear anywhere in the sanitized number. This behavior isn’t ideal, especially if your backend or API expects the ‘+’ sign strictly at the start to indicate the international prefix.

Challenges with the Current Method

One significant limitation of this naive approach is handling edge cases effectively. For example, a user accidentally types characters before the ‘+’ international prefix, like ” 12+3 456–7890 “. In such scenarios, your backend system could face issues interpreting the number or might outright reject it.

Additionally, looping through each character individually with multiple checks may look simple but becomes inefficient at scale. For every character, Kotlin checks and validates conditions, increasing processing overhead—especially problematic with large datasets or batch operations.

Introducing a More Efficient Solution in Kotlin

To handle these scenarios more gracefully, let’s refine our solution. We’ll utilize Kotlin’s powerful regular expression capabilities. Regular expressions help specify exact patterns to replace or filter unwanted characters swiftly and correctly—no matter how messy the input.

Here’s a concise, powerful way to remove unwanted characters and keep the phone number formatted correctly:


fun efficientSanitizePhoneNumber(phone: String): String {
    // Trim spaces and then remove all characters except digits and a leading plus
    val trimmedPhone = phone.trim()
    return trimmedPhone.replace("[^\\d+]".toRegex(), "")
        .replace("(?

Step-by-Step Explanation

Let's break down why this method works better:

  1. Trim Excess Whitespace: Using trim() removes leading/trailing spaces.
  2. Eliminate All Unwanted Characters: The regex [^\\d+] searches for any character that's neither a digit nor a '+' and promptly removes it.
  3. Restrict '+' to the Start: The second regex (? identifies '+' signs not at the start and removes them entirely—making sure it appears only once, at the beginning of an international number.

Now let's compare the results against the original function to see improvements:


Original: '(123)-456 7890' ==> '1234567890'
Original: '+1-800-555-0199' ==> '+18005550199'
Original: '++(001)-23 456!' ==> '+00123456' (extra '+' removed)
Original: ' 12+3 456--7890 ' ==> '1234567890' (moved misplaced '+')

Clearly, the proposed solution performs significantly better at handling edge cases.

Analyzing Efficiency & Performance

Beyond correctly sanitizing your phone numbers, our improved method also offers better performance. Regular expressions in Kotlin are optimized and compiled into efficient internal state machines, significantly speeding up character matching operations.

Your original function had an implicit time complexity of O(n), where 'n' is the length of the input, but it became sluggish requiring multiple checks and operations per character. The proposed regex-based method reduces redundancy and checks only precisely what's necessary, improving real-world performance dramatically, especially in data-heavy applications.

Testing and Validation of Our Improved Function

To ensure the reliability of our new sanitization function, let's test it across a few more challenging cases:


fun main() {
    val sampleNumbers = listOf(
        "+44 20 7946 0958",
        "0044 (207) 946-0958",
        "Invalid+++123456",
        "12-34-56+78++90"
    )

    sampleNumbers.forEach { number ->
        println("Before: '$number' ==> After: '${efficientSanitizePhoneNumber(number)}'")
    }
}

Running this gives us:


Before: '+44 20 7946 0958' ==> After: '+442079460958'
Before: '0044 (207) 946-0958' ==> After: '00442079460958'
Before: 'Invalid+++123456' ==> After: '+123456'
Before: '12-34-56+78++90' ==> After: '1234567890'

All test cases handle unwanted characters cleanly, showcasing robustness and consistency in the new method.

Real-World Applications of Efficient Phone Number Processing

Efficient phone number sanitization plays a crucial part in various real-world scenarios:

  • E-commerce & OTP Systems: Quickly verifying and sanitizing phone numbers ensure SMS notifications, OTP authentications, and delivery updates reach users correctly.
  • CRM & Analytics Platforms: Properly processed phone numbers help businesses analyze and segment customer datasets accurately.
  • Messaging & Calling Apps: Ensuring data consistency prevents accidental communication failures and unnecessary user frustration.

When integrated properly, this regex-based Kotlin function contributes significantly to user satisfaction, data quality, and overall backend efficiency.

Enhance Your App by Adopting Efficient Phone Number Processing

We explored an efficient, accurate method of removing unwanted characters from phone numbers through Kotlin regular expressions. The proposed solution transforms messy, confusing input into reliable, formatted phone numbers quickly and consistently.

With simple implementation, significantly improved performance, and reliability handling even tricky edge cases, there's little reason to stick to less efficient solutions.

Ready to optimize your Kotlin apps further? Start implementing efficient string processing today by adopting robust sanitizing practices—because cleaner data leads to a smoother user experience.

How are you handling phone number sanitization in your Kotlin apps? Have a different method or suggestion? Let us know your thoughts!


Like it? Share with your friends!

Shivateja Keerthi
Hey there! I'm Shivateja Keerthi, a full-stack developer who loves diving deep into code, fixing tricky bugs, and figuring out why things break. I mainly work with JavaScript and Python, and I enjoy sharing everything I learn - especially about debugging, troubleshooting errors, and making development smoother. If you've ever struggled with weird bugs or just want to get better at coding, you're in the right place. Through my blog, I share tips, solutions, and insights to help you code smarter and debug faster. Let’s make coding less frustrating and more fun! My LinkedIn Follow Me on X

0 Comments

Your email address will not be published. Required fields are marked *