作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
将字符串转换为驼峰命名法
例如:“user_id”到“userId”“用户名”到“用户名”“country_province_city”到“countryProvinceCity”如何以简单的方式做到这一点?
ps:“country_province_city”应该是“countryProvinceCity”而不是“countryprovincecity”
最佳答案
我会使用一个循环和一个 StringBuilder
。类似的东西
String[] arr = { "user_id", "user_name", "country_province_city" };
for (String str : arr) {
StringBuilder sb = new StringBuilder(str);
int pos;
while ((pos = sb.indexOf("_")) > -1) {
String ch = sb.substring(pos + 1, pos + 2);
sb.replace(pos, pos + 2, ch.toUpperCase());
}
System.out.printf("%s = %s%n", str, sb);
}
我得到了(要求的)
user_id = userId
user_name = userName
country_province_city = countryProvinceCity
关于java - 如何在Java中将 "user_id"转换为 "userId"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30609523/
我是一名优秀的程序员,十分优秀!