Java tutorial
//package com.java2s; public class Main { private static String getNumericPrefix(String string) { StringBuffer numeric = new StringBuffer(); if (string != null) { string = string.trim(); if (string.length() > 0) { StringBuffer buffer = new StringBuffer(string); char first = buffer.charAt(0); if (Character.isDigit(first)) { numeric.append(first); for (int i = 1; i < buffer.length(); i++) { Character next = buffer.charAt(i); if (Character.isDigit(next)) { numeric.append(next); // skip commas within numbers } else if (next.equals(',')) { continue; } else { break; } } } } } return numeric.length() == 0 ? null : numeric.toString(); } }