How to move the digits of a string with digits and letters to the beginning of the string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"unicode"
)

func moveDigitsToFront(input string) string {
	var digits, others []rune

	for _, char := range input {
		if unicode.IsDigit(char) {
			digits = append(digits, char)
		} else {
			others = append(others, char)
		}
	}

	return string(digits) + string(others)
}

func main() {
	input := "d2c54be3a1"
	result := moveDigitsToFront(input)
	
	fmt.Println(result) 
}

 
 
/*
run:
 
25431dcbea
 
*/

 



answered May 27, 2025 by avibootz
...