zoukankan      html  css  js  c++  java
  • 《Java编程思想》第十三章 字符串

    《Java编程思想》读书笔记

    1、String作为方法的参数时,会复制一份引用,而该引用所指的对象其实一直待在单一的物理位置,从未动过。
    2、显式地创建StringBuilder允许预先为他指定大小。如果知道字符串多长,可以预先指定StringBuilder的大小避免多次重新分配的冲突。
     1 /** 
     2  * @author zlz099: 
     3  * @version 创建时间:2017年9月1日 下午4:03:59 
     4  */
     5 public class UsingStringBuilder {
     6     
     7     public static Random random  = new Random(47);
     8     public String toString(){
     9         StringBuilder result = new StringBuilder("[");
    10         for(int i = 0; i < 25;i++){
    11             result.append(random.nextInt(100));
    12             result.append(". ");
    13         }
    14         result.delete(result.length()-2, result.length());
    15         result.append("]");
    16         return result.toString();
    17     }
    18     public static void main(String[] args) {
    19         // TODO Auto-generated method stub
    20         UsingStringBuilder usb = new UsingStringBuilder();
    21         System.out.println(usb);
    22     }
    23 
    24 }
    3、如果在toString中使用循环,则最好创建一个StringBuilder对象。
    4、String上的操作:如果需要改变字符串的内容时,String类的方法会返回一个新的String对象。同时,如果没有改变内容,则String方法只是返回指向原对象的引用。
     1 import java.io.PrintStream;
     2 import java.util.*;
     3 
     4 /** 
     5  * @author zlz099: 
     6  * @version 创建时间:2017年9月19日 下午3:50:23 
     7  */
     8 public class Turtle {
     9     private String name;
    10     private Formatter f;
    11     public Turtle(String name,Formatter f){
    12         this.name = name;
    13         this.f = f;
    14     }
    15     public void move(int x,int y){
    16         f.format("%s The Turtle is at(%d,%d)
    ",name,x,y);
    17     }
    18     public static void main(String[] args) {
    19         // TODO Auto-generated method stub
    20         PrintStream outAlias = System.out;
    21         Turtle tommy = new Turtle("Tommy", new Formatter(System.out));
    22         Turtle terry = new Turtle("Terry", new Formatter(outAlias));
    23         tommy.move(0, 0);
    24         terry.move(4, 8);
    25         tommy.move(3, 4);
    26         terry.move(2, 5);
    27         tommy.move(3, 3);
    28         terry.move(3, 3);
    29         
    30     }
    31 
    32 }
     1 import java.util.Formatter;
     2 
     3 /** 
     4  * @author zlz099: 
     5  * @version 创建时间:2017年9月19日 下午4:25:05 
     6  */
     7 public class Recipe {
     8     private double total = 0;
     9     private Formatter formatter = new Formatter(System.out);
    10     public void printTitle(){
    11         formatter.format("%-15s %5s %10s
    ", "Item","Qty","Price");
    12         formatter.format("%-15s %5s %10s
    ", "----","---","-----");
    13     }
    14     public void print(String name,int qty,double price){
    15         formatter.format("%-15s %5s %10.2f
    ", name,qty,price);
    16         total += price;
    17     }
    18     public void printTotal(){
    19         formatter.format("%-15s %5s %10.2f
    ", "Tax","",total*0.06);
    20         formatter.format("%-15s %5s %10s
    ", "","","-----");
    21         formatter.format("%-15s %5s %10.2f
    ", "Total","",total*1.06);
    22     }
    23     public static void main(String[] args) {
    24         // TODO Auto-generated method stub
    25         Recipe recipe = new Recipe();
    26         recipe.printTitle();
    27         recipe.print("Jack's Magic Beans", 4, 4.25);
    28         recipe.print("Princess peas", 3, 5.1);
    29         recipe.print("Three Bears Porridge", 1, 14.29);
    30         recipe.printTotal();    
    31     }
    32 }

    十六进制的转储工具:

     1 import java.io.File;
     2 import java.util.function.BinaryOperator;
     3 
     4 /** 
     5  * @author zlz099: 
     6  * @version 创建时间:2017年9月19日 下午4:48:11 
     7  * 十六进制转储工具(dump)
     8  */
     9 public class Hex {
    10     public static String format(byte[] data){
    11         StringBuilder result = new StringBuilder();
    12         int n = 0;
    13         for(byte b:data){
    14             if(n%16==0){
    15                 result.append(String.format("%05X: ", n));
    16             }
    17             result.append(String.format("%02X ", b));
    18             n++;
    19             if(n%16 == 0) result.append("
    ");
    20         }
    21         result.append("
    ");
    22         return result.toString();
    23     }
    24     public static void main(String[] args) throws Exception {
    25         // TODO Auto-generated method stub
    26         if(args.length == 0){
    27             System.out.println(format(BinaryFile.read("Hex.class")));
    28         }
    29         else {
    30             System.out.println(format(BinaryFile.read(new File(args[0]))));
    31         }
    32     }
    33 
    34 }
     1 import java.io.BufferedInputStream;
     2 import java.io.File;
     3 import java.io.FileInputStream;
     4 import java.io.IOException;
     5 import java.nio.BufferUnderflowException;
     6 
     7 /** 
     8  * @author zlz099: 
     9  * @version 创建时间:2017年9月19日 下午4:56:17 
    10  */
    11 public class BinaryFile {
    12     public static byte[] read(File bFile)throws IOException{
    13         BufferedInputStream bf = new BufferedInputStream(new FileInputStream(bFile));
    14         try {
    15             byte[] data = new byte[bf.available()];
    16             bf.read(data);
    17             return data;
    18         } finally {
    19             // TODO: handle exception
    20             bf.close();
    21         }
    22     }
    23     public static byte[] read(String bFile)throws IOException{
    24         return read(new File(bFile).getAbsoluteFile());
    25     }
    26 }
    5、正则表达式:插入一个数字:\d    插入一个反斜线:\\
     1 import java.util.Arrays;
     2 
     3 /** 
     4  * @author zlz099: 
     5  * @version 创建时间:2017年9月20日 上午9:53:58 
     6  */
     7 public class Splitting {
     8     public static String knights = "Then, when you have found the shrubbery. you must "+
     9             "cut down the mightiest tree in the forest... "+
    10             "with... a herring!";
    11     public static void split(String regex){
    12         System.out.println(Arrays.toString(knights.split(regex)));
    13     }
    14     public static void main(String[] args) {
    15         // TODO Auto-generated method stub
    16         split(" ");
    17         split("\W+");
    18         split("n\W+");
    19     }
    20 
    21 }
    6、split()方法:将字符串从正则表达式匹配的地方切开。在原始字符串中,与正则表达式匹配的部分,在最终结果中都不存在。重载版本允许限制字符串分割的次数。
     1 /** 
     2  * @author zlz099: 
     3  * @version 创建时间:2017年9月20日 上午9:57:46 
     4  */
     5 public class Replacing {
     6     static String s = Splitting.knights;
     7     public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9         System.out.println(s.replaceFirst("f\W+", "locaked"));//替换第一个f开头的单词
    10         System.out.println(s.replaceAll("shrubbery|tree|herring", "banana"));//替换所有匹配的,|表示或
    11     }
    12 }
    7、split(" "); 按空格划分   split("\W+");  非单词字符划分    split("n\W+");  n字符后跟一个或多个非单词字符。
    8、replace( ); 可以替换字符,替换正则表达式第一个匹配的子串或者全部替换。
    9、find()可以在输入的任意位置定位正则表达式,lookingAt()和match()只能在正则表达式与输入的开始处就开始匹配时才能成功。match()只有在整个输入匹配正则表达式时成功,lookingAt()只要输入第一部分匹配就能成功。
     1 import java.util.regex.Matcher;
     2 import java.util.regex.Pattern;
     3 
     4 /** 
     5  * @author zlz099: 
     6  * @version 创建时间:2017年9月20日 上午11:11:04 
     7  * 用来在CharSequence中查找多个匹配
     8  */
     9 public class Finding {
    10 
    11     public static void main(String[] args) {
    12         // TODO Auto-generated method stub
    13         Matcher m = Pattern.compile("\w+").matcher("Evening is full of the linnet's wings");
    14         while(m.find()){
    15             System.out.print(m.group()+" ");
    16         }
    17         System.out.println();
    18         int i = 0;
    19         while(m.find(i)){
    20             //接受一个整数作为参数,表示字符串中字符的位置,并以其作为搜索起点。以这个参数作为起点,不断重新设定搜索的起始位置。
    21             System.out.print(m.group()+" ");
    22             i++;
    23         }
    24     }
    25 
    26 }
     1 import java.util.Arrays;
     2 import java.util.regex.Pattern;
     3 
     4 /** 
     5  * @author zlz099: 
     6  * @version 创建时间:2017年9月20日 上午10:25:13 
     7  */
     8 public class SplitDemo {
     9 
    10     public static void main(String[] args) {
    11         // TODO Auto-generated method stub
    12         String input = "This!!unusual use!!of exclamation!!points";
    13         System.out.println(Arrays.toString(Pattern.compile("!!").split(input)));
    14         //第二个split限制将输入分割成字符串的数量
    15         System.out.println(Arrays.toString(Pattern.compile("!!").split(input,3)));
    16     }
    17 }
    10、appendReplacement(StringBuffer sbuf,String replacement) :允许在执行替换的过程中操作用来替换的字符串。
    11、Scanner构造器可以接受任何类型的输入对象,包括File对象。有了Scanner,所有的输入、分词以及翻译的操作都放在不同类型的next()方法中。hasNext()方法可以判断下一个输入分词是否所需的类型。
     1 import java.io.BufferedReader;
     2 import java.io.StringReader;
     3 
     4 /** 
     5  * @author zlz099: 
     6  * @version 创建时间:2017年9月20日 上午10:39:32 
     7  */
     8 public class SimpleRead {
     9     public static BufferedReader input = new BufferedReader(new StringReader("Sir Robin of Camelot
    22 1.61803"));
    10     public static void main(String[] args) {
    11         // TODO Auto-generated method stub
    12         try {
    13             System.out.println("What is your name?");
    14             String name = input.readLine();
    15             System.out.println(name);
    16             System.out.println("How old are you? What is your favorite double?");
    17             System.out.println("(input: <age> <double>");
    18             String numbers = input.readLine();
    19             System.out.println(numbers);
    20             String[] numArray = numbers.split(" ");
    21             int age = Integer.parseInt(numArray[0]);
    22             double favorite = Double.parseDouble(numArray[1]);
    23             System.out.format("Hi %s.
    ",name);
    24             System.out.format("In 5 years you will be %d.
    ",age+5);
    25             System.out.format("My favorite double is  %f.
    ",favorite/2);
    26         } catch (Exception e) {
    27             // TODO: handle exception
    28             System.err.println("I/O exception");
    29         }
    30     }
    31 
    32 }
    12、Scanner有一个假设,在输入结束时会自动抛出IOException,所以Scanner将IOException吞掉了。
     1 import java.util.Scanner;
     2 
     3 /** 
     4  * @author zlz099: 
     5  * @version 创建时间:2017年9月20日 上午10:50:07 
     6  */
     7 public class BetterRead {
     8 
     9     public static void main(String[] args) {
    10         // TODO Auto-generated method stub
    11         Scanner stdin = new Scanner(SimpleRead.input);
    12         System.out.println("What is your name?");
    13         String name = stdin.nextLine();
    14         System.out.println(name);
    15         System.out.println("How old are you? What is your favorite double?");
    16         System.out.println("(input: <age> <double>");
    17         int age = stdin.nextInt();
    18         double favorite = stdin.nextDouble();
    19         System.out.println(age);
    20         System.out.println(favorite);
    21         System.out.format("Hi %s.
    ",name);
    22         System.out.format("In 5 years you will be %d.
    ",age+5);
    23         System.out.format("My favorite double is  %f.
    ",favorite/2);
    24 
    25     }
    26 
    27 }
     
  • 相关阅读:
    经典音乐插曲推荐![附地址]
    广播电台常用51首背景音乐——绝对经典
    酒吧..夜店常用歌曲~潮人必备音乐噢~【附下载地址】
    ASP与ASP.NET互通COOKIES的一点经验
    linux进程状态浅析
    常用的酒吧经典乐曲106首
    呼和浩特电视台媒资管理系统的设计与分析
    不再为DataGrid生成的表格的单无格中的内容过长、自动折行、表格撑开等问题而烦恼
    Linux shell脚本全面学习
    ASP.NET文件管理显示信息
  • 原文地址:https://www.cnblogs.com/zlz099/p/7560469.html
Copyright © 2011-2022 走看看