Java中Split方法和Scanner類

2021-09-01 13:48:04 字數 559 閱讀 9275

split函式和scanner類都可以使用正規表示式實現字串分割。

例如從命令列接受一行字串如: 1 2(注意1前面有多個空格,1與2之間有多個空格)

split:

inputstreamreader isr=new inputstreamreader(system.in);

bufferedreader br=new bufferedreader(isr);

string a;

trycatch (ioexception e)

此時,字串陣列a中,有3個元素,分別是空格,1,2。

scanner:

scanner sc=new scanner(system.in);

int x=sc.nextint();

int y=sc.nextint();

sc.close();

則x=1,y=2.

具體可以查閱jdk幫助文件,更加詳細。

java 中 split 方法應用

這裡講的split方法是string類中的方法,用來分割字串,它返回乙個字串陣列string string類中部分原始碼如下 public string split string regex,int limit public string split string regex 呼叫 string s...

join 方法和split 方法

b 是乙個空位元組表示用空位元組連線位元組串,python3新的地方,以前join只能連線字串,現在可以連線位元組串 python字串轉成位元組串三種方法 str zifuchuang 第一種 b zifuchuang 第二種bytes zifuchuang encoding utf 8 第三種 z...

java中split的用法

private static void testsplit 執行後你會發現結果是 5,並不是8,檢視原始碼可知會 從尾部檢查字元的長度是否為空,為空的就刪掉了,size遞減,只檢查尾部,頭部不檢查,所以頭部的 存在。下面是一下練習題 aa12sas32sasa223sas12as12wqe 去掉數字...