Difference between revisions of "Remove non-numbers from a string"

From CodeCodex

(Java)
Line 6: Line 6:
 
     return s.replaceAll("[A-Z]|[a-z]|[ !@#\\$%\\^&\\*\\(\\)_\\+\\-={}\\|:\"<>\\?\\-=\\[\\];',\\./`~'£€¥]","");
 
     return s.replaceAll("[A-Z]|[a-z]|[ !@#\\$%\\^&\\*\\(\\)_\\+\\-={}\\|:\"<>\\?\\-=\\[\\];',\\./`~'£€¥]","");
 
  }
 
  }
 +
===OCaml===
 +
# let remove_nondigit = Str.global_replace (Str.regexp "[^0-9]+") "";;
 +
val remove_nondigit : string -> string = <fun>
 +
For example:
 +
# remove_nondigit "133t H4x0r";;
 +
- : string = "13340"
  
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:String]]
 
[[Category:String]]

Revision as of 02:34, 11 February 2007

Related content:

Implementations

Java

This code removes common symbols and numbers from a string, returning letters only. (Note, not all symbols are included.)

static String numbersOnly(String s) {
   return s.replaceAll("[A-Z]|[a-z]|[ !@#\\$%\\^&\\*\\(\\)_\\+\\-={}\\|:\"<>\\?\\-=\\[\\];',\\./`~'£€¥]","");
}

OCaml

# let remove_nondigit = Str.global_replace (Str.regexp "[^0-9]+") "";;
val remove_nondigit : string -> string = <fun>

For example:

# remove_nondigit "133t H4x0r";;
- : string = "13340"