public class StringReplace {
public static void main(String[] args) {
String message = "Hi everyone!";
System.out.println("message = " + message.replace("e", "X"));
}
}
What is the result?
A.
message = Hi everyone!
B.
message = Hi XvXryonX!
C. A compile time error is produced.
D. A runtime error is produced.
E.
message =
F.
message = Hi Xveryone!
題解
字串物件的replace方法會將字串裡特定的字元或是子字串,全都取代成其它的字串。因此:
Hi everyone! -> Hi XvXryonX!
除了replace方法之外還有快速取代字串中符合正規表示式的子字串,所用的replaceAll方法以及replaceFirst方法。