java 輸出包含逗號的字符串中的每一個字符
問題描述
public void evaluateCourse() {//Res res = new Res();//Connection con = null;String total = '1,2,33,53';for(int i = 0;i < total.length() ; i ++){ if(total.charAt(i) != ’,’){System.out.println(total.charAt(i)); }} }
輸出結(jié)果是
我想要的結(jié)果是: 1
2 33 53
問題解答
回答1:先不管用不用String.split()
public void evaluateCourse() {//Res res = new Res();//Connection con = null;String total = '1,2,33,53';String temp = '';for(int i = 0;i < total.length() ; i ++){ temp += total.charAt(i); if(total.charAt(i) == ’,’){System.out.println(temp);temp = ''; }} }
再看看用split的
public void evaluateCourse() {//Res res = new Res();//Connection con = null;String total = '1,2,33,53';String[] temp = aa.split(',');for(String s : temp) { System.out.println(s);} }回答2:
為什么不用String.split()?
回答3:Stream.of(total.split(',')).forEach(System.out::println);
相關(guān)文章:
1. python - beautifulsoup獲取網(wǎng)頁內(nèi)容的問題2. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題3. docker鏡像push報(bào)錯4. docker - 如何修改運(yùn)行中容器的配置5. docker-machine添加一個已有的docker主機(jī)問題6. fragment - android webView 返回后怎么禁止重新渲染?7. docker不顯示端口映射呢?8. android studio總是在processes running好久9. dockerfile - [docker build image失敗- npm install]10. angular.js - Angular 2 + Django構(gòu)建的Web應(yīng)用, 如何合理搭配 ?
