java 字符串

1、不可变性

String对象一旦创建以后是不可以修改的,它具有只读特性,所有指向它的应用是不可能改变的它的值的。

 1 /**
 2  * java 字符串
 3  * 2016/5/8
 4  */
 5 package cn.Java_7;
 6 
 7 public class Think_13_4 {
 8 
 9     public static void main(String[] args) {
10         String s1 = "hello";
11         String s2 = "hello";
12         String s3;
13         s3 = s2;
14         String s4 = new String("hello");
15         String s5 = new String("hello");
16         System.out.println(s1 == s2);   //true
17         System.out.println(s2 == s3);   //true
18         System.out.println(s2 == s4);   //false
19         System.out.println(s4 == s5);   //false
20         System.out.println(s4.equals(s5));    //true
21     }
22 
23 }

     String s1这种创建字符串时,创建的是引用,所以16、17行是相等的为true;

    String s4 = new String();这种字符串对象的创建是新开辟一个地址空间去存储这个字符串,所以19行的为false。

2、格式化输出

 1 /**
 2  * java  格式化输出
 3  * 2016/5/8
 4  **/
 5 package cn.Java_7;
 6 
 7 import java.util.Formatter;
 8 
 9 public class Think_13_5 {
10 
11     
12     public static void main(String[] args) {
13         int x = 5;
14         double y = 5.1234;
15         System.out.println("x=" + x + "  y= " +y);
16         System.out.format("x=" + x + "  y= " + y);
17         System.out.printf("\nx=" + x + "  y= " + y);
18         
19         String s = "who are you";
20         Formatter fm = new Formatter(System.out);
21         fm.format("\nx=%d  y=%2.3e  s=%s",x,y,s);
22 
23     }
24 
25 }

 3、正则表达式

1)String的matches方法

 1 /**
 2  * java  正则表达式
 3  * 2016/5/8
 4  **/
 5 package cn.Java_7;
 6 
 7 import java.util.*;
 8 
 9 public class Think_13_6 {
10     public static void main(String[] args) {
11         System.out.println("-1234".matches("-?\\d+"));      //true
12         System.out.println("1234".matches("-?\\d+"));       //true
13         System.out.println("+1234".matches("\\+?\\d+"));    //true
14     }
15 }

 

2)String的split方法

 1 /**
 2  * java  正则表达式
 3  * 2016/5/8
 4  **/
 5 package cn.Java_7;
 6 
 7 import java.util.*;
 8 
 9 public class Think_13_6 {
10     
11     public static String str =
12             "Then, when you have found the shrubbery, you must "+
13              "cut down the mightiest tree in the fotest with ... a herriong!";
14     
15     public static void main(String[] args) {
16         System.out.println("-1234".matches("-?\\d+"));      //true
17         System.out.println("1234".matches("-?\\d+"));       //true
18         System.out.println("+1234".matches("\\+?\\d+"));    //true
19         System.out.println("1234".matches("(-|\\+)?\\d+"));
20         //方式一
21         Split("");
22         Split(" ");
23         Split("\\W+");
24         Split("n\\W+");
25         Split("[n]");
26         //方式二
27         String str_1 = "zhonghuaonhaaa";
28         System.out.println(Arrays.toString(str_1.split("[o|h]")));
29         System.out.println(Arrays.toString(str_1.split("[on]")));
30         System.out.println(str_1.matches("zhonghuaonhaaa"));
31         System.out.println(str_1.matches("z.*"));
32         System.out.println(str_1.matches("[zZAa]honghuaonhaaa"));
33         System.out.println(str_1.matches("[zZ][hH][a-z].*"));
34     }
35 
36     public static void Split(String regex){
37         
38         System.out.println(Arrays.toString(str.split(regex)));
39     }
40 
41 }

 

 运行结果:

3)Pattern和Matcher方法

 1 /**
 2  * java  正则表达式 Pattern 和 Matcher
 3  * 2016/5/9
 4  **/
 5 package cn.Java_7;
 6 
 7 import java.util.regex.*;
 8 import java.util.regex.Pattern;
 9 public class Think_13_6_4 {
10 
11     public static void main(String[] args) {
12         String strs = "abcabcabcadefhabc";        //将要匹配的字符串
13         String ptt = "(abc){2}";        //匹配规则 [abcabc]
14         Matcher mc = Pattern.compile(ptt).matcher(strs);        //调用Pattern与strs进行匹配
15         int i = 0;                        //有多少相匹配的
16         while(mc.find()){
17             System.out.println("Match \"" + mc.group() + "\" at positions " + mc.start() + "-" + (mc.end()-1));
18             i++;
19         }
20         System.out.println("共匹配到"+i+"个字符串");
21     }
22 
23 }

 

运行结果:

4)替换操作

replaceFirst只能替换第一个匹配到的字符串。

replaceAll可以替换所有匹配到的字符串。

appendReplacement是渐进式替换。

 1 /**
 2  * java 正则表达式之替换操作
 3  * 2016/5/9
 4  **/
 5 package cn.Java_7;
 6 
 7 import java.util.regex.Matcher;
 8 import java.util.regex.Pattern;
 9 
10 public class Think_13_6_6 {
11 
12     public static void main(String[] args) {
13         //进行匹配看一下,还未替换
14         String strs = "abcabcabcadefhabc";        //将要匹配的字符串
15         System.out.println("strs:" + strs);
16         String ptt = "abc";        //匹配规则 [abcabc]
17         Matcher mc = Pattern.compile(ptt).matcher(strs);        //调用Pattern与strs进行匹配
18         int i = 0;                        //有多少相匹配的
19         while(mc.find()){
20             System.out.println("Match \"" + mc.group() + "\" at positions " + mc.start() + "-" + (mc.end()-1));
21             i++;
22         }
23         System.out.println("共匹配到"+i+"个字符串");
24         
25         String strs_1 = strs.replaceFirst("abc", "***");    //进行替换  替换第一个匹配掉的字符串
26         System.out.println("strs_1:" + strs_1);
27         
28         String strs_2 = strs.replaceAll("abc", "***");    //进行替换  替换第一个匹配掉的字符串
29         System.out.println("strs_2:" + strs_2);
30         
31         String pt = "[aeiou]";        //匹配规则 [abcabc]
32         StringBuffer sbuf = new StringBuffer();
33         Matcher m = Pattern.compile(pt).matcher(strs);        //调用Pattern与strs进行匹配
34         while(m.find()){
35             m.appendReplacement(sbuf, m.group().toUpperCase());//  将[aeiou]转换为大写
36         }
37         m.appendTail(sbuf);    //在执行了一次或多次appendRepalcement()之后,将输入的字符串剩余部分复制到sbuf中
38         System.out.println(sbuf);
39     }
40 
41 }

 

运行结果:

 

posted on 2016-05-09 23:34  snail-lb  阅读(207)  评论(0编辑  收藏  举报