《THING IN JAVA》 13章字符串-读书笔记

来源:互联网 发布:百万公众网络测试入口 编辑:程序博客网 时间:2024/05/16 08:40

1、字符串的不可变性质

String对象是不可变的,String类的每一个方法实际上都是产生一个新的String对象进行返回


2、String与StringBuilder

采用String对象的“+”连接对象,在虚拟机的原理实质上是新建了一个String对象,在返回的对象包含加上原来的字符一并返回

StringBuilder是字符串对象,所提供的方法会改变原来的对象


如果一段程序中包含了许多的字符串相加的语句,会严重效率,因为会产生大量的String对象

StringBuilder是线性非安全的,StringBuffer是线性安全的


3、Formatter类

Formatter类,关于格式化的一类参数

创建Formatter类对象时,给构造器传递一个输出流对象

用format()方法告诉该类需要什么样的格式输出内容

String的静态方法format(),内部是创建了一个Formatter对象,然后将你的参数传入Formatter()对象,最后返回一个String对象


实际应用中用得更为多的是在 java.text 包下继承抽象类Format下的方法

如SimpleDateFormat,DecimalFormat 都是一些处理格式化不错的类

下面是几种格式话的应用例子


public class MyTest1 {
public static void main(String[] args){
int a = 3;
Formatter f = new Formatter(System.out);
f.format("%d is true\n", a);

int aa = 8888;
System.out.println(String.format("%X",aa));

DecimalFormat d1 = new DecimalFormat("#0.00");
double b = 3.124423232;
System.out.println(b);
System.out.println(d1.format(b));

SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println(date);
System.out.println(d2.format(date));

f.close();
}
}


输出结果:


3 is true
22B8
3.124423232
3.12
Sun Jan 01 15:44:07 CST 2017
2017-01-01


4、正则表达式String.spil(),Pattern、Matcher类

比起功能有限的String类,我们更愿意构造功能强大的正则表达式对象

Pattern,Matcher类位于 java.util.regex包


首先用Pattern类的静态方法static Pattern.compile()方法编译你的正则表达式 

Pattern Pattern.compile(String regex,int flag)

然后调用Pattern类的matcher()方法,参数为你要检索的字符串,然后会返回一个Matcher类对象

Matcher类对象有很多反法,具体可以看java.util.regex包的说明文件


Pattern类的静态方法还有一个版本,它接受一个标记参数,以调整匹配的行为

Pattern Pattern.compile(String regex,int flag)

最实用的几个标记

Pattern.CASE_INSENSITIVE(?i)  忽略大小写

Pattern.MULTILINE(?m) 多行模式下,匹配每行的开头

Pattern.COMMENTS(?x) 忽略空格


String的spil()方法用于分割字符串

Pattern也有静态的spit() 方法

除了Matcher的matcher()方法

String也有非静态方法matches()用于匹配两个字符串是否吻合


下面是几种实例的输出:


public class MyTest2 {
public static void main(String[] args){
String message = "Java has regex aa#aa\n"
 + "java is good aa#aa\n"
 + "Regular are bed aa#aa\n"
 + "JAVA is big";
f1(message);
f2(message);
f3(message);
f4(message);
f5(message);
}

public static void f1(String message){
System.out.println("This is the methon f1----------");
Pattern p  = Pattern.compile("^java");
printInfo(p,message);
}

public static void f2(String message){
System.out.println("This is the methon f2----------");
Pattern p = Pattern.compile("^java",Pattern.CASE_INSENSITIVE);
printInfo(p,message);
}

public static void f3(String message){
System.out.println("This is the methon f3----------");
Pattern p = Pattern.compile("^java",Pattern.CASE_INSENSITIVE 
| Pattern.MULTILINE);
printInfo(p,message);
}

public static void f4(String message){
System.out.println("This is the methon f4---------");
Pattern p = Pattern.compile("aa#aa");
System.out.println(Arrays.toString(p.split(message)));
}

public static void f5(String message){
System.out.println("This is the methon f5--------");
System.out.println(Arrays.toString(message.split("aa#aa")));
}

public static void printInfo(Pattern p,String message){
Matcher m = p.matcher(message);
int i = 0;
while(m.find()){
System.out.println("start:"+m.start());
System.out.println("匹配组"+m.group());
System.out.println("end:"+m.end());
i++;
}
System.out.println("共匹配了"+i+"个字符");
}
}


输出结果:


This is the methon f1----------
共匹配了0个字符
This is the methon f2----------
start:0
匹配组Java
end:4
共匹配了1个字符
This is the methon f3----------
start:0
匹配组Java
end:4
start:21
匹配组java
end:25
start:62
匹配组JAVA
end:66
共匹配了3个字符
This is the methon f4---------
[Java has regex , 
java is good , 
Regular are bed , 
JAVA is big]
This is the methon f5--------
[Java has regex , 
java is good , 
Regular are bed , 
JAVA is big]

0 0