gpt4 book ai didi

java - 如何在Java中将 "user_id"转换为 "userId"?

转载 作者:行者123 更新时间:2023-12-01 07:04:12 25 4
gpt4 key购买 nike

将字符串转换为驼峰命名法

例如:“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com