此代码适用于测试..但是当它重复数字时,这不会给出有效的输出..
例如这个**(ancxyz123,3,5,XYZ)**输入我得到ancXYZ123作为输出而不是 ancXYZ1235本应该是输出< /强> ..
这是关于检查字符串并打印它而没有任何特殊字符并复制字符,如果整个字符串中的大写字母中存在任何字符,它应该以大写字母打印该字符。。
公共课Test1222015 {
public static void main(String... strings) {
String s = "ancxyz123, 3,5, XYZ";
String s1 = StringTest(s);
System.out.println(s1);
}
public static String StringTest(String s) {
// TODO Auto-generated method stub
int sLength = s.length();
StringBuilder sb = new StringBuilder();
boolean found = false;
boolean previously = false;
boolean uppercase = false;
if (s != null && sLength > 1) {
for (int i = 0; i < sLength; i++) {
char temp = s.charAt(i);
if (Character.toString(temp).matches("[a-zA-Z\\d\\s]")) {
if (Character.isAlphabetic(temp)) {
for (int j = 0; j < i; j++) {
char tempinner = s.charAt(j);
if (Character.isAlphabetic(tempinner)) {
if (Character.toLowerCase(temp) == Character
.toLowerCase(tempinner)) {
previously = true;
break;
}
}
}
for (int j = i + 1; j < sLength; j++) {
char tempinner = s.charAt(j);
if (Character.isAlphabetic(tempinner)) {
if (Character.toLowerCase(temp) == Character
.toLowerCase(tempinner)) {
found = true;
break;
}
}
}
if (!previously && !found) {
sb.append(temp);
}
else if (!previously && found) {
for (int j = i + 1; j < sLength; j++) {
char assignCheck = s.charAt(j);
if (Character.isAlphabetic(assignCheck)) {
if (Character.toLowerCase(temp) == Character
.toLowerCase(assignCheck)) {
uppercase = Character
.isUpperCase(assignCheck);
if (uppercase) {
break;
}
}
}
}
if (uppercase) {
sb.append(Character.toUpperCase(temp));
} else {
sb.append(temp);
}
}
} // alpha if ends here
else if (Character.isDigit(temp)) {
System.out.println(temp);
for (int j = 0; j < i; j++) {
char tempinner = s.charAt(j);
if (Character.isDigit(tempinner)) {
if (temp == tempinner) {
previously = true;
break;
}
}
}
if (!previously) {
sb.append(temp);
}
/* this edited Piece Made it Work */
previously=false;
}
else {
sb.append(" ");
}
}// test condition if ends here
// ends outer for loop
}
}// ends outer if condition
return sb.toString();
}
}
请在不使用太多循环和条件的情况下向我推荐一些很好的方法来完成这项工作
谢谢:) 编辑: **
**
但请建议一些好方法来做这项工作并避免这么多循环和所有
答案 0 :(得分:0)
您可能会发现以下answers in StackOverflow有用。我看到两种类型的解决方案。一个使用 regular expressions
,另一个使用 StringBuffer
。