看板 [ java ]
討論串[問題] String 取特定的字
共 5 篇文章
首頁
上一頁
1
下一頁
尾頁

推噓0(0推 0噓 0→)留言0則,0人參與, 最新作者liyard (Keyword)時間16年前 (2009/03/26 23:37), 編輯資訊
0
0
0
內容預覽:
這只是個簡潔的寫法. 當作好玩就好,不用太在意. String str = "011001";. System.out.println(str.replaceAll("[^1]", "").length());. 優點就是一行搞定(炸). 印出0就代表沒有,就這麼簡單. --. 發信站: 批踢踢

推噓0(0推 0噓 0→)留言0則,0人參與, 最新作者adrianshum (Alien)時間16年前 (2009/03/26 15:41), 編輯資訊
0
0
0
內容預覽:
Java5 或以上的話. String str = "10110";. int count = 0;. for (char c : str.toCharArray()) {. if (c == '1') {. ++count;. } else if (c != '0') {. // System.o

推噓0(0推 0噓 0→)留言0則,0人參與, 最新作者adrianshum (Alien)時間16年前 (2009/03/26 15:35), 編輯資訊
0
0
0
內容預覽:
檢查: inputNumber.matches("^[01]+$"). 要 count 的話, 用 charAt loop 一次算是比較快的.. 硬要寫短的話,. input.replace("0", "").length(). 或 input.split("1").size -1 也可. --.

推噓0(0推 0噓 0→)留言0則,0人參與, 最新作者windknife18 (windknife18)時間16年前 (2009/03/26 15:24), 編輯資訊
0
0
0
內容預覽:
String str = "101110";. int count = 0;. for(int i=0 ; i<str.length() ; i++){. if(str.charAt(i) == '1'). count = count + 1;. }. System.out.println("You

推噓4(4推 0噓 0→)留言4則,0人參與, 最新作者blackall (犽★)時間16年前 (2009/03/26 13:11), 編輯資訊
0
0
0
內容預覽:
要讀取二進位數值後. 要show出裡面共有幾個1. 在判斷二進位數值後 若不是二進位數值要重新輸入. 這邊都還ok. 但是要算我的字串有幾個1的時候我就卡住了=^=. 我想到是用IndexOf判斷有沒有1的這個數. 之後就卡死@@" 判斷有沒有1之後我要怎麼樣count有幾個1. EX:. whil
(還有108個字)
首頁
上一頁
1
下一頁
尾頁