字符串中的无效字符
Invalid characters in String
我正在尝试将包含连字符和下划线的字符串输入运行sform 成驼峰式大小写。
示例:
the-stealth-warrior
应该变成
theStealthWarrior
我以为我完全理解了这个问题,直到我 运行 在我 运行 代码时遇到一些奇怪的无效输出问题。
我的代码:
public class Application {
public static void main(String[] args) {
System.out.println(toCamelCase("the-stealth-warrior"));
}
public static String toCamelCase(String s) {
int sLength = s.length();
char[] camelCaseWord = new char[sLength];
int camelCaseIndex = 0;
for (int i = 0; i < sLength; i++) {
if (s.charAt(i) == '-' || s.charAt(i) == '_') {
char upper = Character.toUpperCase(s.charAt(i + 1));
camelCaseWord[camelCaseIndex] = upper;
camelCaseIndex++;
i++;
} else {
camelCaseWord[i] = s.charAt(i);
camelCaseIndex++;
}
}
return new String(camelCaseWord);
}
}
我的输出:
theS tealtW arrior
有谁知道可能是什么问题?
谢谢
改为
camelCaseWord[camelCaseIndex] = s.charAt(i);
你也应该检查这个
char upper = Character.toUpperCase(s.charAt(i + 1));
确保在连字符或下划线位于字符串末尾的情况下不超过字符串的边界
你也可以这样做(我认为它不那么复杂):
private static void covertToCamelCase(String s) {
String camelCaseString = "";
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '_' || s.charAt(i) == '-'){
// if hyphen or underscore is at the beggining
if(i == 0) {
camelCaseString += s.toLowerCase().charAt(i+1);
} // if hyphen or underscore is at the end
else if(i != s.length()-1) {
camelCaseString += s.toUpperCase().charAt(i + 1);
}
i++;
}else{
camelCaseString += s.charAt(i);
}
}
System.out.println(camelCaseString);
}
当你运行代码
public static void main(String[] args) {
covertToCamelCase("test_test");
covertToCamelCase("some-new-string-word");
covertToCamelCase("hyphen-at-end-");
covertToCamelCase("-hyphen-at-beginning-");
}
输出将是:
testTest
someNewStringWord
hyphenAtEnd
hyphenAtBeginning
我正在尝试将包含连字符和下划线的字符串输入运行sform 成驼峰式大小写。
示例:
the-stealth-warrior
应该变成
theStealthWarrior
我以为我完全理解了这个问题,直到我 运行 在我 运行 代码时遇到一些奇怪的无效输出问题。
我的代码:
public class Application {
public static void main(String[] args) {
System.out.println(toCamelCase("the-stealth-warrior"));
}
public static String toCamelCase(String s) {
int sLength = s.length();
char[] camelCaseWord = new char[sLength];
int camelCaseIndex = 0;
for (int i = 0; i < sLength; i++) {
if (s.charAt(i) == '-' || s.charAt(i) == '_') {
char upper = Character.toUpperCase(s.charAt(i + 1));
camelCaseWord[camelCaseIndex] = upper;
camelCaseIndex++;
i++;
} else {
camelCaseWord[i] = s.charAt(i);
camelCaseIndex++;
}
}
return new String(camelCaseWord);
}
}
我的输出:
theS tealtW arrior
有谁知道可能是什么问题? 谢谢
改为
camelCaseWord[camelCaseIndex] = s.charAt(i);
你也应该检查这个
char upper = Character.toUpperCase(s.charAt(i + 1));
确保在连字符或下划线位于字符串末尾的情况下不超过字符串的边界
你也可以这样做(我认为它不那么复杂):
private static void covertToCamelCase(String s) {
String camelCaseString = "";
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '_' || s.charAt(i) == '-'){
// if hyphen or underscore is at the beggining
if(i == 0) {
camelCaseString += s.toLowerCase().charAt(i+1);
} // if hyphen or underscore is at the end
else if(i != s.length()-1) {
camelCaseString += s.toUpperCase().charAt(i + 1);
}
i++;
}else{
camelCaseString += s.charAt(i);
}
}
System.out.println(camelCaseString);
}
当你运行代码
public static void main(String[] args) {
covertToCamelCase("test_test");
covertToCamelCase("some-new-string-word");
covertToCamelCase("hyphen-at-end-");
covertToCamelCase("-hyphen-at-beginning-");
}
输出将是:
testTest
someNewStringWord
hyphenAtEnd
hyphenAtBeginning