+的重載于StringBuilder
關(guān)于string的+,它的源碼雖然沒(méi)有使用StringBuilder 類,但是編譯器自動(dòng)引入使用了他,因?yàn)樗咝В贿^(guò)使用+的性能還是不如直接自己創(chuàng)建一個(gè)StringBuilder對(duì)象。
- Collectors.joining() 內(nèi)部也是使用的 StringBuilder
// strings/UsingStringBuilder.java
import java.util.*;
import java.util.stream.*;
public class UsingStringBuilder {
public static String string1() {
Random rand = new Random(47);
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 String string2() {
String result = new Random(47)
.ints(25, 0, 100)
.mapToObj(Integer::toString)
.collect(Collectors.joining(", "));
return "[" + result + "]";
}
public static void main(String[] args) {
System.out.println(string1());
System.out.println(string2());
}
}
/* Output:
[58, 55, 93, 61, 61, 29, 68, 0, 22, 7, 88, 28, 51, 89,
9, 78, 98, 61, 20, 58, 16, 40, 11, 22, 4]
[58, 55, 93, 61, 61, 29, 68, 0, 22, 7, 88, 28, 51, 89,
9, 78, 98, 61, 20, 58, 16, 40, 11, 22, 4]
*/
格式化輸出
// strings/SimpleFormat.java
public class SimpleFormat {
public static void main(String[] args) {
int x = 5;
double y = 5.332542;
// The old way:
System.out.println("Row 1: [" + x + " " + y + "]");
// The new way:
System.out.format("Row 1: [%d %f]%n", x, y);
// or
System.out.printf("Row 1: [%d %f]%n", x, y);
}
}
/* Output:
Row 1: [5 5.332542]
Row 1: [5 5.332542]
Row 1: [5 5.332542]
*/
- Formatter類
在 Java 中,所有的格式化功能都是由 java.util.Formatter 類處理的??梢詫?Formatter 看做一個(gè)翻譯器,它將你的格式化字符串與數(shù)據(jù)翻譯成需要的結(jié)果。當(dāng)你創(chuàng)建一個(gè) Formatter 對(duì)象時(shí),需要向其構(gòu)造器傳遞一些信息,告訴它最終的結(jié)果將向哪里輸出:
// strings/Turtle.java
import java.io.*;
import java.util.*;
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);
terry.move(4,8);
tommy.move(3,4);
terry.move(2,5);
tommy.move(3,3);
terry.move(3,3);
}
}
/* Output:
Tommy The Turtle is at (0,0)
Terry The Turtle is at (4,8)
Tommy The Turtle is at (3,4)
Terry The Turtle is at (2,5)
Tommy The Turtle is at (3,3)
Terry The Turtle is at (3,3)
*/

image.png
正則表達(dá)式
在其他語(yǔ)言中,\\ 表示“我想要在正則表達(dá)式中插入一個(gè)普通的(字面上的)反斜線,請(qǐng)不要給它任何特殊的意義?!倍贘ava中,\\ 的意思是“我要插入一個(gè)正則表達(dá)式的反斜線,所以其后的字符具有特殊的意義。”例如,如果你想表示一位數(shù)字,那么正則表達(dá)式應(yīng)該是 \\d。如果你想插入一個(gè)普通的反斜線,應(yīng)該這樣寫(xiě) \\\。不過(guò)換行符和制表符之類的東西只需要使用單反斜線:\n\t。
// strings/IntegerMatch.java
public class IntegerMatch {
public static void main(String[] args) {
System.out.println("-1234".matches("-?\\d+"));
System.out.println("5678".matches("-?\\d+"));
System.out.println("+911".matches("-?\\d+"));
System.out.println("+911".matches("(-|\\+)?\\d+"));
}
}
/* Output:
true
true
false
true
*/
- String類還自帶了一個(gè)非常有用的正則表達(dá)式工具——split() 方法,其功能是“將字符串從正則表達(dá)式匹配的地方切開(kāi)。”
package 劍指0ffer.java編程思想.字符串;
import java.util.Arrays;
public class Splitting {
public static String knights =
"Then, when you have found the shrubbery, " +
"you must cut down the mightiest tree in the " +
"forest...with... a herring!";
public static void split(String regex) {
System.out.println(
Arrays.toString(knights.split(regex)));
}
public static void main(String[] args) {
split(" ");
split("\\W+");
split("n\\W+");
}
}
用正則表達(dá)式進(jìn)行替換操作時(shí),你可以只替換第一處匹配,也可以替換所有的匹配
public class Replacing {
static String s = Splitting.knights;
public static void main(String[] args) {
System.out.println(
s.replaceFirst("f\\w+", "located"));
System.out.println(
s.replaceAll("shrubbery|tree|herring","banana"));
}
}
/* Output:
Then, when you have located the shrubbery, you must cut
down the mightiest tree in the forest...with... a
herring!
Then, when you have found the banana, you must cut down
the mightiest banana in the forest...with... a banana!
*/

image.png