import java.util.regex.Pattern; /** * @Description: 将月份或字符串中的汉字去掉,只剩下数字。 * @Author: ls @Date: 17:05 17:05 */ public class StringTrimAsNum { public static void main(String[] args) { String str = "1月"; String str1 = "6月"; String result = Pattern.compile("[^0-9]").matcher(str1).replaceAll(""); System.out.println(result); } }
输出结果:6java