Java中split的用法

【示例程序】

public class Split {
	public static String[]ss=new String[20];
	
	public Split(){
		String s="The rain in Spain falls mainly in the plain.";
		ss=s.split(" ");
	}
	
	public static void main(String args[]){
		Split demo=new Split();
		for (int i=0;i<ss.length;i++)
			System.out.println(ss[i]);
	}
}


【示例程序】

public class Split1 {
	public static void main(String args[]){
		String[]ss=new String[20];
		
		
		String s="The rain in Spain falls mainly in the plain.";
		ss=s.split(" ",3);
		
		for (int i=0;i<ss.length;i++)
			System.out.println(ss[i]);
	}
}


【示例程序】

import java.io.*;

public class HotelInfo {
	public static void main(String args[])throws IOException{
		FileReader fr=new FileReader("input1.txt");
		FileWriter fw=new FileWriter("output.txt");
		BufferedReader br=new BufferedReader(fr);
		PrintWriter pw=new PrintWriter(fw);
		String s;
		String []ss=new String[20];
		
		while((s=br.readLine())!=null){
			ss=s.split("\\|");
			double n=Double.parseDouble(ss[2]);
			pw.println(ss[0]+"  "+ss[1]+"  "+ss[2]);
		}
		
		br.close();
		pw.close();
	}
}

一些轉義字符

 \\ 反斜槓 
\t 間隔 ('\u0009')
\n 換行 ('\u000A')
\r 回車 ('\u000D')
\d 數字 等價於 [0-9]
\D 非數字 等價於 [^0-9]
\s 空白符號 [\t\n\x0B\f\r]
\S 非空白符號 [^\t\n\x0B\f\r]
\w 單獨字符 [a-zA-Z_0-9]
\W 非單獨字符 [^a-zA-Z_0-9]
\f 換頁符 
\e Escape
\b 一個單詞的邊界 
\B 一個非單詞的邊界 
\G 前一個匹配的結束


.要用轉義字符\\.表示,|要用轉義字符\\|表示。


發佈了46 篇原創文章 · 獲贊 33 · 訪問量 61萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章