1.无意识的递归
package com.lhsjohn.pr.string;
import java.util.ArrayList;
import java.util.List;
/**
* 迭代打印本类的地址信息,在这里可以把所有的宽度提取成常量,需要修改时只需要修改一处即可。
* @author lihuashuo
*
*/
public class InfiniteRecursion {
public String toString() {
//return "InfiniteRecursion address: " + this + "\n"; 这样写会报错,原因是将this转换成String时无意识调用了this的toString(),造成递归
return "InfiniteRecursion address: " + super.toString() + "\n";
}
public static void main(String[] args) {
List<InfiniteRecursion> v=new ArrayList<InfiniteRecursion>();
for( int i=0; i<10; i++ )
v.add(new InfiniteRecursion());
System.out.println(v);
}
}
输出结果:
[InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@70dea4e
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@5c647e05
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@33909752
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@55f96302
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@3d4eac69
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@42a57993
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@75b84c92
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@6bc7c054
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@232204a1
, InfiniteRecursion address: com.lhsjohn.pr.string.InfiniteRecursion@4aa298b7
]
2.Formatter类的格式化输出
package com.lhsjohn.pr.string;
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.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);
}
}
输出结果:
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)
3.修改Turtle.java,使之将结果输出到System.err中
package com.lhsjohn.pr.string;
/**
* 修改Turtle.java 将结果输出到System.err中
* @author lihuashuo
*
*/
import java.io.PrintStream;
import java.util.Formatter;
public class Turtle2 {
private String name;
private Formatter f;
public Turtle2(String name, Formatter f) {
this.name = name;
this.f = f;
}
public void move(int x,int y) {
f.format(" %s Turtle is at (%d,%d)\n",name,x,y);
}
public static void main(String[] args) {
PrintStream err = System.err;
Turtle2 tommy = new Turtle2("tommy",
new Formatter(System.err));
Turtle2 terry = new Turtle2("terry",
new Formatter(err));
tommy.move(0, 0);
terry.move(4, 8);
tommy.move(3, 4);
terry.move(2, 5);
tommy.move(3, 3);
terry.move(3, 3);
}
}
输出结果:
//字体变成红色
tommy Turtle is at (0,0)
terry Turtle is at (4,8)
tommy Turtle is at (3,4)
terry Turtle is at (2,5)
tommy Turtle is at (3,3)
terry Turtle is at (3,3)
4.我们来格式化打印一个购物收据
package com.lhsjohn.pr.string;
import java.util.Formatter;
/**
* 格式化说明符来打印一个收据
* @author lihuashuo
*
*/
public class Receipt {
private double total=0;
private Formatter f= new Formatter(System.out);
public void printTitle() {
f.format("%-15s %5s %10s\n","Item","Qty","Price");
f.format("%-15s %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 %10.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();
}
}
输出结果:
Item Qty Price
---- --- -----
Jack's Magic Be 4 4.25
Princess Peas 3 5.10
Three Bears Por 1 14.29
Tax 1.42
-----
Total 25.06
5.正则表达式入门
package com.lhsjohn.pr.string;
/**
* 应用正则表达式匹配相应规则的整数
* @author lihuashuo
*
*/
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+"));
}
}
输出结果:
true
true
false
true
6.String类自带的正则表达式工具--split()方法,将字符串从正则表达式匹配的地方切开
package com.lhsjohn.pr.string;
import java.util.Arrays;
/**
* 将字符串从正则表达式匹配的地方切开
* 与正则表达式匹配的部分都不存在了
* @author lihuashuo
*
*/
public class Splitting {
public static String knight=
"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(knight.split(regex)));
}
public static void main(String[] args) {
//根据空格来划分字符串
split(" ");
// \W表示非单词字符,这个可以删除标点符号
split("\\W+");
// 字母n后面跟着一个或多个非单词字符
split("n\\W+");
}
}
输出结果
[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest..., with..., a, herring!]
[Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring]
[The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest... with... a herring!]
7.String.split的重载版本,允许限制字符串分割的次数
package com.lhsjohn.pr.string;
/**
* 限制字符串分割的次数
* @author lihuashuo
*
*/
public class Replacing {
static String s = Splitting.knight;
public static void main(String[] args) {
//以字母f开头,后面跟着一个或者多个字母,并且只能替换掉第一个匹配的部分,在这里"found"替换成了"located"
System.out.println(s.replaceFirst("f\\w+", "located"));
//匹配的是三个单词中的任意一个,替换所有匹配的部分
System.out.println(s.replaceAll("shrubbery|tree|herring", "banana"));
}
}
输出结果:
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!
8.编写一个正则表达式,检测一个句子是否以大写字母开头
package com.lhsjohn.pr.string;
public class Sentence7 {
public static void main(String[] args) {
//检查一个句子是否以大写字母开头,以句号结尾
String sen="^[A-Z].*[\\.]$";
String s1="Once upon a time.";
String s2="abcd.";
String s3="Abcd?";
String s4="An easy way out.";
String s5="Zorro.";
String s6="X.";
System.out.println(s1.matches(sen));
System.out.println(s2.matches(sen));
System.out.println(s3.matches(sen));
System.out.println(s4.matches(sen));
System.out.println(s5.matches(sen));
System.out.println(s6.matches(sen));
}
}
输出结果:
true
false
false
true
true
true
9.将字符串Splitting.knight在the和you处分割
package com.lhsjohn.pr.string;
import java.util.Arrays;
public class Splitting8 {
public static String knight=
"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(knight.split(regex)));
}
public static void main(String[] args) {
split("the|you");
}
}
输出结果:
[Then, when , have found , shrubbery, , must cut down , mightiest tree in , forest... with... a herring!]
10.用下划线替换Splitting.knight中的所有元音字母
package com.lhsjohn.pr.string;
public class Replacing9 {
public static String knight=
"Then, when you have found the shrubbery, you must "+
"cut down the mightiest tree in the forest... "+
"with... a herring!";
public static void main(String[] args) {
System.out.println(knight.replaceAll("[aeiouAEIOU]", "_"));
}
}
输出结果:
Th_n, wh_n y__ h_v_ f__nd th_ shr_bb_ry, y__ m_st c_t d_wn th_ m_ght__st tr__ _n th_ f_r_st... w_th... _ h_rr_ng!
作者:lhsjohn
网友评论