将字符串转换为camelCase
例如: “user_id”改为“userId” “user_name”到“userName” “country_province_city”改为“countryProvinceCity”
如何以简单的方式做到这一点?
ps:“country_province_city”应该是“countryProvinceCity”而不是“countryprovincecity”
答案 0 :(得分:3)
正如Fast Snail所提到的那样,只需使用String str = "user_id, user_name, user_id";
,如果str = str.replaceAll("userID", "user_id");
,请致电str
,导致"userID, user_name, userID"
现在拥有值public String toCamel(String str) {
String[] splits = str.split("_");
for (int i = 1; i < splits.length; i++) {
char first = Character.toUpperCase(splits.charAt(0));
if (splits[i].length() > 0)
splits[i] = first + splits[i].substring(1);
else
splits[i] = first + "";
}
String toRet = "";
for (String s : splits)
toRet += s;
return toRet;
}
或者,更完整的方法如下
{{1}}
答案 1 :(得分:3)
我会使用循环和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
答案 2 :(得分:1)
这是一个非常简单的问题:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String result = "";
String input = scan.nextLine();
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '_') {
result += input.toUpperCase().charAt(i + 1);
i = i + 1;
} else {
result += input.toLowerCase().charAt(i);
}
}
System.out.println(result);
}
如果您喜欢多次这样做,我建议您使用while循环来反复重复相同的代码:
while (true) {
//the previous code
}
答案 3 :(得分:1)
http://commons.apache.org/proper/commons-lang/javadocs/api-3.4/index.html
import urllib2 as ur
import re
f = ur.urlopen(u'http://www.site.co.uk/sitemap.xml')
res = f.readlines()
for d in res:
data = re.findall('<loc>(http:\/\/.+)<\/loc>',d)
for i in data:
print i
file = open("sitemapdata.txt", "a")
file.write(i +'\n')
file.close()
输出 countryProvinceCity
答案 4 :(得分:0)
从另一个角度来看,上面的答案也可以使用split
函数和两个loops
来完成,如下所示:
String[] strings = {"user_id","user_name","country_province_city"};
for (int i = 0; i < strings.length; i++)
{
String string = strings[i];
String totalString = "";
String[] divide = string.split("_");
for(int j = 0; j < divide.length; j++)
{
if(j != 0)
{
divide[j] = "" + divide[j].toUpperCase().charAt(0) + divide[j].substring(1,divide[j].length());
}
totalString = totalString + divide[j];
}
}
如果您想通过控制台显示此更改Strings
,您只需在第二个循环后添加System.out.println
并在第一个循环内添加for (int i = 0; i < strings.length; i++)
{
//The same code as the code that I put in the example above
for(int j = 0; j < divide.length; j++)
{
//The same code as the example above
}
System.out.println(totalString);
}
,如下所示:
array
相反,如果你的目标是将它们存储到String[] store;
for (int i = 0; i < strings.length; i++)
{
//The same code as the code that I put in the example above
store = new String[divide.length];
for(int j = 0; j < divide.length; j++)
{
//The same code as the example above
}
store[j] = totalString;
}
中,你可以这样做:
$hide.addClass('hide').removeClass('show');
$show.removeClass('hide').addClass('show');
如果您对代码有任何疑问,请告诉我。
我希望它会对你有所帮助!