问题与练习:字符和字符串
Questions
- 以下字符串 生成器的初始容量是多少?
StringBuilder sb = new StringBuilder("Able was I ere I saw Elba.");
- 考虑以下字符串:
String hannah = "Did Hannah see bees? Hannah did.";
-
表达式
hannah.length()
显示的值是多少?-
方法调用
hannah.charAt(12)
返回的值是多少? -
在指向
hannah
的字符串 中写一个指向字母b
的表达式。
-
-
以下表达式返回的字符串 多 Long 时间?什么是琴弦?
"Was it a car or a cat I saw?".substring(9, 12)
- 在下面的名为ComputeResult的程序中,每行编号执行后
result
的值是什么?
public class ComputeResult {
public static void main(String[] args) {
String original = "software";
StringBuilder result = new StringBuilder("hi");
int index = original.indexOf('a');
/*1*/ result.setCharAt(0, original.charAt(0));
/*2*/ result.setCharAt(1, original.charAt(original.length()-1));
/*3*/ result.insert(1, original.charAt(4));
/*4*/ result.append(original.substring(1,4));
/*5*/ result.insert(3, (original.substring(index, index+2) + " "));
System.out.println(result);
}
}
Exercises
- 显示两种方法将以下两个字符串 连接在一起以获得字符串
"Hi, mom."
:
String hi = "Hi, ";
String mom = "mom.";
-
编写一个程序,使用您的全名来计算您的姓名首字母并将其显示出来。
-
字谜是通过换位另一个单词或短语的字母而形成的单词或短语;例如,“议会”是“偏男人”的字谜,“软件”是“誓言”的字谜。编写一个程序,弄清楚一个字符串 是否是另一个字符串 的字谜。该程序应忽略空格和标点符号。