Java-字符串

1. 不可變String

String是不可變的,String類中每一個看起來會修改String值的方法,實際上都是創建了一個全新的String對象,以包含修改後的字符串內容。而最初的String對象則絲毫未動

<code>public class Immutable {
public static String upcase(String s){
return s.toUpperCase();
}

public static void main(String[] args) {
String q = "howdy";
System.out.println(q);
String qq = upcase(q);
System.out.println(qq);
System.out.println(q);
}
}/<code>

當把q傳給upcase()方法時,實際傳遞的是引用的一個拷貝。其實,每當把String對象作為方法的參數時,都會複製一份引用,而該引用所指的對象其實一直待在單一的物理位置上,從未動過。

回到upcase()的定義,傳入其中的引用有了名字s,只有upcase()運行的時候,局部引用s才存在。一旦upcase()運行結束,s就消失了。當然了,upcase()的返回值,其實只是最終結果的引用。這足以說明,upcase()返回的引用已經指向了一個新的對象,而原本的q則還在原地

2. 重載“+” 和StringBuilder

重載的意思是,一個操作符在引用於特定的類時,被賦予了特殊的意義(用於String的“+”與“+=”是Java中僅有的兩個重載過的操作符,而Java並不允許成員重載任何操作符)

操作符“+”可以用來連接String:

<code>public class Contatenation {
public static void main(String[] args) {
String mango = "mango";
String s = "abc" + mango + "def" + 47;
System.out.println(s);
}
}/<code>
<code>public class UsingStringBuilder {
public static Random rand = new Random(47);
public String toString(){
StringBuilder result = new StringBuilder("[");
for(int i = 0; i < 25;i++){
result.append(rand.nextInt(100));
result.append(", ");
}

result.delete(result.length()-2,result.length());
result.append("]");
return result.toString();
}

public static void main(String[] args) {
UsingStringBuilder usb = new UsingStringBuilder();
System.out.println(usb);
}
}/<code>

StringBuilder提供了豐富而全面的方法,包括insert()、replace()、substring()甚至reverse(),但最常用的還是append()和toString()。還有delete()方法。

3.格式化輸出

JavaSE5引入的format方法可用於PrintStream或PrintWriter對象,其中也包括System.out對象。

<code>public class SimpleFormat {
public static void main(String[] args) {
int x = 5;
double y = 5.332542;
System.out.println("Row 1: [ " + x + " " + y + "]");
System.out.format("Row 1: [%d %f]\\n" ,x,y);
System.out.printf("Row 1: [%d %f]\\n" ,x,y);
}
}/<code>

format()與printf()是等價的,它們只需要一個簡單的格式化字符串,加上一串參數即可,每個參數對應一個格式化修飾符

在Java中,所有新的格式化功能都由java.util.Formatter 類處理。可以將Formatter看作一個翻譯器,它將你的格式化字符串與數據翻譯稱需要的結果,當你創建一個Formatter對象的時候,需要向其構造器傳遞一些信息,告訴它最終的結果將向哪裡輸出

<code>public class Turtle {
private String name;
private Formatter f;
public Turtle(String name,Formatter f){
this.name = name;
this.f = f;
}

public void move(int x,int y){
f.format("%s The Turtle is at (%d,%d)\\n",name,x,y);
}

public static void main(String[] args) {
PrintStream outAlias = System.out;
Turtle tommy = new Turtle("Tommy",new Formatter(System.out));
Turtle terry = new Turtle("terry",new Formatter(outAlias));
tommy.move(0,0);
tommy.move(3,4);
tommy.move(3,3);
terry.move(4,8);
terry.move(2,5);
terry.move(3,3);
}
}/<code>

4. 掃描輸入

從文本或標準輸入讀取數據還是一件相當痛苦的事情。一般的解決之道就是讀入一行文本,對其進行分詞,然後使用Integer、Double等類的各種解析方法來解析數據

<code>public class SimpleRead {
public static BufferedReader input = new BufferedReader(new StringReader("Sir Robin of Camelot\\n 22 1.61803"));

public static void main(String[] args) {
try {
System.out.println("What is your name?");
String name = input.readLine();
System.out.println(name);
System.out.println("How old are you ? what is your favorite double?");
System.out.println("(input: <double>");
String numbers = input.readLine();
System.out.println(numbers);
String[] numArray = numbers.split(" ");
int age = Integer.parseInt(numArray[0]);
double favorite = Double.parseDouble(numArray[1]);
System.out.format("Hi %s.\\n",name);
System.out.format("In 5 years you will be %d.\\n",age+5);
System.out.format("My favorite double is %f.",favorite/2);
} catch (IOException e) {
e.printStackTrace();
}
}
}/<double>
/<code>

Scanner的構造器可以接受任何類型的輸入對象,包括File對象、InputStream、String或者下面例子中的Readable對象。

<code>public class BetterRead {
public static void main(String[] args) {
Scanner stdin = new Scanner(SimpleRead.input);
System.out.println("What is your name?");

String name = stdin.nextLine();
System.out.println(name);
System.out.println("How old are you ? what is your favorite double?");
System.out.println("(input: <double>");

int age = stdin.nextInt();
double favorite = stdin.nextDouble();
System.out.println(age);
System.out.println(favorite);
System.out.format("Hi %s.\\n",name);
System.out.format("In 5 years you will be %d.\\n",age+5);
System.out.format("My favorite double is %f.",favorite/2);

}
}/<double>
/<code>


分享到:


相關文章: