Extract maximum numeric value from a given string | Set 1 ( General approach ) | Utility method to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Method to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Driver method
function maximumNum ( curr_num , res ) { let len1 = curr_num . length ; let len2 = res . length ; if ( len1 == len2 ) { let i = 0 ; while ( curr_num [ i ] == res [ i ] ) i ++ ; if ( curr_num [ i ] < res [ i ] ) return res ; else return curr_num ; } return len1 < len2 ? res : curr_num ; } function extractMaximum ( str ) { let n = str . length ; let curr_num = " " ; let res = " " ; for ( let i = 0 ; i < n ; i ++ ) { while ( i < n && str [ i ] == ' ' ) i ++ ; while ( i < n && ! isNaN ( String ( str [ i ] ) * 1 ) ) { curr_num = curr_num + str [ i ] ; i ++ ; } if ( i == n ) break ; if ( curr_num . length > 0 ) i -- ; res = maximumNum ( curr_num , res ) ; curr_num = " " ; } if ( curr_num . length == 0 && res . length == 0 ) res = res + ' ' ; return maximumNum ( curr_num , res ) ; } let str = " " ; document . write ( extractMaximum ( str ) ) ;