[英]Better way to invert cases of characters in a string in Java
As a novice Java programmer who barely got started in Java programming, I am totally exhausted in trying to find a solution to this issue.作为一个刚开始接触 Java 编程的新手 Java 程序员,我在试图找到解决这个问题的方法时完全筋疲力尽。 A course that I am currently studying gave homework that asked me to create a Java class that has a sort of “reverse” method that returns a new version of the string of the current string where the capitalization is reversed (ie, lowercase to uppercase and uppercase to lowercase) for the alphabetical characters specified in a given condition.
我目前正在学习的一门课程的作业要求我创建一个 Java class 具有一种“反向”方法,该方法返回当前字符串的字符串的新版本,其中大小写被反转(即,小写到大写和大写到小写)用于在给定条件中指定的字母字符。 Say if I were to reverse “abc, XYZ;
假设我要反转“abc, XYZ; 123.”
123。” using reverse("bcdxyz@3210."), it must return "aBC, xyz; 123.".
使用 reverse("bcdxyz@3210."),它必须返回 "aBC, xyz; 123."。 (PS: the class ignores numbers and special characters and the variable "myString" is where the "abc, XYZ; 123." goes to.).
(PS:class 忽略数字和特殊字符,变量“myString”是“abc, XYZ; 123.”所在的位置。)。 So far, I've only managed to return out "aBC, XYZ; 123."
到目前为止,我只设法返回“aBC, XYZ; 123”。 with the code below.
使用下面的代码。 Am I missing something here?
我在这里错过了什么吗?
public String reverse(String arg) {
// TODO Implement method
String arg_no_sym = arg.replaceAll("[^a-zA-Z0-9]","");
String arg_perfect = arg_no_sym.replaceAll("\\d","");
if (myString != null) {
char[] arrayOfReplaceChars = arg_perfect.toCharArray();
char[] arrayOfmyString = myString.toCharArray();
for (int i = 0; i < arg_perfect.length(); i++) {
myString = myString.replace(String.valueOf((arrayOfReplaceChars[i])), String.valueOf((arrayOfReplaceChars[i])).toUpperCase());
}
return myString;
}
else {
return "";
}
}
How about using the methods isUpperCase()
and isLowerCase()
to check the case of the letters and then use toUpperCase()
and toLowerCase()
to change the case of them?如何使用方法
isUpperCase()
和isLowerCase()
检查字母的大小写,然后使用toUpperCase()
和toLowerCase()
更改它们的大小写?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.