Move all digits to the beginning of a given string | Java program for the above approach ; Function to move all the digit to the beginning of the string ; Calculate the string length ; Stores the digits ; Stores the non - numeric character ; Traverse the string and check if there is a digit ; If character is a digit , add it to the string digits ; Otherwise , add it to the string nonNumericCharacter ; Append both the strings ; Print the string ; Driver Code ; Given String str
class GFG { static void moveAllDigitAtBeginning ( String str ) { int len = str . length ( ) ; StringBuilder digits = new StringBuilder ( ) ; StringBuilder nonNumericCharacter = new StringBuilder ( ) ; for ( char c : str . toCharArray ( ) ) { if ( c >= 48 && c <= 57 ) { digits . append ( c ) ; } else { nonNumericCharacter . append ( c ) ; } } digits . append ( nonNumericCharacter . toString ( ) ) ; System . out . print ( digits . toString ( ) + " ▁ " ) ; } public static void main ( String args [ ] ) { String str = " GeeksforGeeks123" ; moveAllDigitAtBeginning ( str ) ; } }