Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,181 questions

56,073 answers

573 users

How to extract all digits from a string in Swift

1 Answer

0 votes
import Foundation

/*
    extractDigits
    -------------
    Walks through the input string and collects only characters
    that are recognized as digits.

    Swift's Character.isNumber provides Unicode‑aware digit detection.
    The function uses a simple and efficient filter operation
    followed by String construction.
*/
func extractDigits(_ s: String) -> String {
    // Filter characters that are digits and build a new string
    let digits = s.filter { $0.isNumber }
    return String(digits)
}

//
// Main execution
//
let str = "5 rustc#8go 9001 c c++ 17python"

// Extract digits
let digits = extractDigits(str)

// Display results
print("Original: \(str)")
print("Digits:   \(digits)")


/*
run:

Original: 5 rustc#8go 9001 c c++ 17python
Digits:   58900117

*/

 



answered 15 hours ago by avibootz
...