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

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

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to extract and sort numbers from a string containing numbers and text in Kotlin

1 Answer

0 votes
/**
 * Program to extract and sort integer values embedded within a string containing text.
 * Demonstrates Kotlin's standard library extensions, regular expressions, and 
 * sequence-based processing.
 */

/**
 * Extracts all contiguous sequences of digits from the provided string and converts
 * them into a list of 64-bit integers.
 *
 * @param input The raw string containing text and embedded numbers.
 * @return A [List] of [Long] numbers in their original order of appearance.
 */
fun extractNumbers(input: String): List<Long> {
    // Regex pattern matching one or more contiguous digits (\d+)
    val digitRegex = Regex("""\d+""")

    // 1. findAll(input) returns a Sequence<MatchResult> of non-overlapping matches.
    //    Using sequences enables lazy evaluation, which avoids unnecessary intermediate allocations.
    // 2. map transformation accesses each matched string value and parses it safely to Long.
    // 3. toList() materializes the transformed sequence into an immutable List.
    return digitRegex.findAll(input)
        .map { match -> match.value.toLong() }
        .toList()
}

/**
 * Sorts a list of numbers in ascending order.
 *
 * @param numbers The list of numbers to sort.
 * @return A new list containing the numbers sorted in ascending order.
 */
fun sortNumbers(numbers: List<Long>): List<Long> {
    // sorted() uses an optimized dual-pivot Quicksort / Timsort algorithm under the hood (O(N log N)).
    // It returns a new immutable sorted list while keeping the input collection untouched.
    return numbers.sorted()
}

fun main() {
    val inputStr = "1000withz7 and3 or 99 give42"

    // Raw string template formatting for output display
    println("Input String:      \"$inputStr\"")

    // Extract numbers using regular expressions and sequences
    val extractedNums = extractNumbers(inputStr)
    println("Extracted Numbers: $extractedNums")

    // Sort the extracted numbers into a new list
    val sortedNums = sortNumbers(extractedNums)
    println("Sorted Numbers:    $sortedNums")
}



/*
run:

Input String:      "1000withz7 and3 or 99 give42"
Extracted Numbers: [1000, 7, 3, 99, 42]
Sorted Numbers:    [3, 7, 42, 99, 1000]

*/

 



answered 1 day ago by avibootz
...