String对象是不可变的。String类中每个看起来会修改String值得方法,实际上都是建立了一个全新得String对象,以包含修改后得字符串内容。java
Java中每一个类从根本上都是继承自Object,标准容器类天然也不例外。
使用toString()方法打印出对象得内存地址正则表达式
import java.util.ArrayList; import java.util.List; public class InfiniteRecursion { public static void main(String[] args) { List<AAA> v = new ArrayList<AAA>(); for (int i = 0; i < 10; i++) v.add(new AAA()); System.out.println(v); } } class AAA { public String toString() { return " InfiniteRecursion address: " + super.toString() + "\n"; } }
printf("Row 1:[%d %f]\n",x,y);
占位符称做格式修饰符,它们说明了插入数据的位置,还说明了插入数据的类型。
format()与printf()是等价的。数组
当你建立一个Formatter对象的时候,须要向其构造器传递一些信息,告诉它最终结果在哪里输出。工具
import java.io.PrintStream; import java.util.Formatter; 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.err; Turtle tommy = new Turtle("Tommy", new Formatter(System.err)); 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); } }
Formatter的构造器通过重载能够接受多种输出目的地,最经常使用的仍是PrintString(),OutputStream和File。测试
%[argument_index$][flags][width][.precision]conversion
width用来控制一个域的最小尺寸。默认下,数据是右对齐,不过能够经过使用“-”标志来改变对齐方向。
precision指示最大大小。用于string表示字符最大数量,用于浮点表示小数部分要显示出来的位数,不可用于整数。this
import java.util.*; public class Receipt { private static final int width=15; private double total = 0; private Formatter f = new Formatter(System.out); public void printTitle() { f.format("%-"+width+"s %"+(width-10)+"s %"+(width-5)+"s\n", "Item", "Qty", "Price"); f.format("%-"+width+"s %5s %10s\n", "----", "---", "-----"); } public void print(String name, int qty, double price) { f.format("%-15.15s %5d %10.2f\n", name, qty, price); total += price; } public void printTotal() { f.format("%-15s %5s %"+(width-5)+".2f\n", "Tax", "", total*0.06); f.format("%-15s %5s %10s\n", "", "", "-----"); f.format("%-15s %5s %10.2f\n", "Total", "", total * 1.06); } public static void main(String[] args) { Receipt receipt = new Receipt(); receipt.printTitle(); receipt.print("Jack's Magic Beans", 4, 4.25); receipt.print("Princess Peas", 3, 5.1); receipt.print("Three Bears Porridge", 1, 14.29); receipt.printTotal(); } }
import java.math.*; import java.util.*; public class Conversion { public static void main(String[] args) { Formatter f = new Formatter(System.out); char u = 'a'; System.out.printf("%3s : %s\n","u","a"); System.out.println("u = 'a'"); f.format("s: %s\n", u); // f.format("d: %d\n", u); f.format("c: %c\n", u); f.format("b: %b\n", u); // f.format("f: %f\n", u); // f.format("e: %e\n", u); // f.format("x: %x\n", u); f.format("h: %h\n", u); int v = 121; System.out.println("v = 121"); f.format("d: %d\n", v); f.format("c: %c\n", v); f.format("b: %b\n", v); f.format("s: %s\n", v); // f.format("f: %f\n", v); // f.format("e: %e\n", v); f.format("x: %x\n", v); f.format("h: %h\n", v); BigInteger w = new BigInteger("50000000000000"); System.out.println( "w = new BigInteger(\"50000000000000\")"); f.format("d: %d\n", w); // f.format("c: %c\n", w); f.format("b: %b\n", w); f.format("s: %s\n", w); // f.format("f: %f\n", w); // f.format("e: %e\n", w); f.format("x: %x\n", w); f.format("h: %h\n", w); double x = 179.543; System.out.println("x = 179.543"); // f.format("d: %d\n", x); // f.format("c: %c\n", x); f.format("b: %b\n", x); f.format("s: %s\n", x); f.format("f: %f\n", x); f.format("e: %e\n", x); // f.format("x: %x\n", x); f.format("h: %h\n", x); Conversion y = new Conversion(); System.out.println("y = new Conversion()"); // f.format("d: %d\n", y); // f.format("c: %c\n", y); f.format("b: %b\n", y); f.format("s: %s\n", y); // f.format("f: %f\n", y); // f.format("e: %e\n", y); // f.format("x: %x\n", y); f.format("h: %h\n", y); boolean z = false; System.out.println("z = false"); // f.format("d: %d\n", z); // f.format("c: %c\n", z); f.format("b: %b\n", z); f.format("s: %s\n", z); // f.format("f: %f\n", z); // f.format("e: %e\n", z); // f.format("x: %x\n", z); f.format("h: %h\n", z); } }
String.format()是一个static方法,它接受与Formatter.format()方法同样的参数,但返回一个String对象。rest
public class DatabaseException extends Exception { public DatabaseException(int transactionID, int queryID, String message) { super(String.format("(t%d, q%d) %s", transactionID, queryID, message)); } public static void main(String[] args) { try { throw new DatabaseException(3, 7, "Write failed"); } catch(Exception e) { System.out.println(e); } } }
String.format()内部也是建立了一个Formatter对象,而后将传入的参数转给Formatter。code
正则表达式提供一种彻底通用的方式,来解决字符串匹配,选择,编辑和验证。orm
使用split()方法分割字符串:对象
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(" "); // Doesn't have to contain regex chars split("\\W+"); // Non-word characters split("n\\W+"); // 'n' followed by non-word characters QA7.getString(); QA7.E9(); } }
使用String自带的正则表达式工具替换:
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")); } }
下面每个表达式都能匹配字符串"Rudolph":
public class Rudolph { public static void main(String[] args) { for (String pattern : new String[] { "Rudolph", "[rR]udolph", "[rR][aeiou][a-z]ol.*", "R.*" }) System.out.println("Rudolph".matches(pattern)); } }
咱们的目的并非编写最难理解的正则表达式,而是尽可能编写可以完成任务的最简单的最必要的正则表达式。
量词描述了一个模式吸取输入文本的方式:
用static Pattern.compile()方法来编译正则表达式,它会根据String类型的正则表达式生成一个Pattern对象,把你要检索的字符串传入Pattern对象的matcher()方法。matcher()方法会生成一个Matcher对象。
测试正则表达式,看它们是否匹配一个输入字符串:
Matcher.find()方法可用来查找多个匹配:
组是用括号划分的正则表达式,能够根据组的编号来引用某个组。组号为0表示整个表达式,组号为1表达被一对括号括起的组。
在匹配操做成功后,start()返回先前匹配的其实位置的索引,而end()返回所匹配的最后字符的索引加一的值。
Pattern类的compile()方法还有一个版本,它接受一个参数,以调整匹配行为:
Pattern Pattern.compile(String regex,int flag)
经过"或"操做符组合多个标记功能:
Split()方法将输入字符串断开成字符串对象数组。
按照通用边界断开文本:
正则表达式特别便于替换文本。
经过reset()方法,将现有的Matcher对象应用于一个新的字符串序列:
输出的是有匹配的部分以及匹配部分在行中的位置:
使用Scanner类扫描输入:
用正则表达式指定本身所需的定界符: