我有兴趣迭代(re:查找和替换目的),说:
List<String> someList = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
someList已经在早期的方法中填充了,并且只包含几个元素,称之为[a:bX,b:Xc],
感兴趣的查找和替换字符串,例如:
String someString = "X";
String otherString = "Y";
String contentsTBD = "";
Run Code Online (Sandbox Code Playgroud)
现在,理想情况下我认为我可以像这样迭代someList:
public void readAndReplace() {
for (int i = 0; i < someList.size(); i++) {
if (someList.get(i).contains(someString)) {
someList.get(i).replace(someString, otherString);
} else {
++i;
}
}
System.out.print(someList);
}
Run Code Online (Sandbox Code Playgroud)
其中打印输出应为:
[a:bY, b:Yc]
Run Code Online (Sandbox Code Playgroud)
然后,我认为这可能有效:
public void readAndReplace() {
for (String s : someList) {
contentsTBD += s;
}
for (int i = 0; i < contentsTBD.length(); i++) {
if (contentsTBD.contains(someString)) {
contentsTBD.replaceAll(someString, …
Run Code Online (Sandbox Code Playgroud) 我感兴趣的是一个非常简单的字符串验证问题,看看字符串中的起始字符是否以大写字母开头,然后让控制台显示true或false.根据我的理解,你不必调用类似System.console().printf("true",s)的东西来实现这一点.我可以发誓我已经看到使用以下示例代码实现了类似的基本实现:
public class Verify {
public static boolean checkStartChar(String s) {
if (s.startsWith("[A-Z]")) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String str = "abCD";
checkStartChar(str);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,没有任何显示.如果我在返回T/F之前添加条件打印输出稍作修改,例如
public class Verify2 {
public static boolean checkStartChar(String s) {
if (s.startsWith("[A-Z]")) {
System.out.println("yep");
return true;
}
else {
System.out.println("nope");
return false;
}
}
public static void main(String[] args) {
String str = "abCD";
checkStartChar(str);
}
}
Run Code Online (Sandbox Code Playgroud)
问题有所解决,因为控制台显示"yep"或"nope",但尚未解决,因为我只想让控制台显示true或false.而已.建议吗?