通过截取开头的“ get”或“ is”并通过正则表达式降低其余字母的首字母,从getter名称中获取属性名称
//package com.nowjava; public class Main { /* *来 自 nowjava.com - 时代Java */ /** * Gets property name from getter name by cutting leading "get" or "is" and lowering the first letter of the rest * * @param methodName Getter name (e.g. "getFirstName" or "isHot") * @return Property name (e.g. "firstName" or "hot") */ public static String getPropertyNameByMethodName(String methodName) { String propertyName = methodName.replaceFirst("^(get|is)", ""); return propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1); } }