Flask接口签名sign原理与实例代码浅析
467
2023-01-26
java驼峰转换的方法
本文实例为大家分享了java驼峰转换的具体代码,供大家参考,具体内容如下
将"_"转换成驼峰,将驼峰转换成"_"。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 驼峰转换
* @author 胡汉三
* 2017年1月19日 下午4:42:58
*/
public class BeanHump {
//转变的依赖字符
public static final char UNDERLINE='_';
/**
* 将驼峰转换成"_"(userId:user_id)
* @param param
* @return
*/
public static String camelToUnderline(String param){
if (param==null||"".equals(param.trim())){
return "";
}
int len=param.length();
StringBuilder sb=new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c=param.charAt(i);
if (Character.isUpperCase(c)){
sb.append(UNDERLINE);
sb.append(Character.toLowerCase(c));
}else{
sb.append(c);
}
}
return sb.toString();
}
/**
* 将"_"转成驼峰(user_id:userId)
* @param param
* @return
*/
public static String underlineToCamel(String param){
if (param==null||"".equals(param.trim())){
return "";
}
int len=param.length();
StringBuilder sb=new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c=param.charAt(i);
if (c==UNDERLINE){
if (++i sb.append(Character.toUpperCase(param.charAt(i))); } }else{ sb.append(c); } } return sb.toString(); } /** * 将"_"转成驼峰(user_id:userId) * @param param * @return */ public static String underlineToCamel2(String param){ if (param==null||"".equals(param.trim())){ return ""; } StringBuilder sb=new StringBuilder(param); Matcher mc= Pattern.compile(UNDERLINE+"").matcher(param); int i=0; while (mc.find()){ int position=mc.end()-(i++); String.valueOf(Character.toUpperCase(sb.charAt(position))); sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase()); } return sb.toString(); } /* * 测试 */ public static void main(String[] args) { System.out.println(camelToUnderline("userNameAll")); System.out.println(underlineToCamel("user_name_all")); System.out.println(underlineToCaNNUrZmel2("user_name_all")); } } 运行结果:
sb.append(Character.toUpperCase(param.charAt(i)));
}
}else{
sb.append(c);
}
}
return sb.toString();
}
/**
* 将"_"转成驼峰(user_id:userId)
* @param param
* @return
*/
public static String underlineToCamel2(String param){
if (param==null||"".equals(param.trim())){
return "";
}
StringBuilder sb=new StringBuilder(param);
Matcher mc= Pattern.compile(UNDERLINE+"").matcher(param);
int i=0;
while (mc.find()){
int position=mc.end()-(i++);
String.valueOf(Character.toUpperCase(sb.charAt(position)));
sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
}
return sb.toString();
}
/*
* 测试
*/
public static void main(String[] args) {
System.out.println(camelToUnderline("userNameAll"));
System.out.println(underlineToCamel("user_name_all"));
System.out.println(underlineToCaNNUrZmel2("user_name_all"));
}
}
运行结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~