Re: [問題] 計算陣列內的字串長度及分割加字?

看板java作者 (人比人Cheese人)時間10年前 (2015/01/13 01:36), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串8/9 (看更多)
※ 引述《ClareQ (人比人Cheese人)》之銘言: : ※ 引述《yuffy0327 (魚排)》之銘言: : : 問題1:有兩個變數,分別是字串 aa 和 字串陣列 ab : :     希望能夠生出字串 ac = "1,0_1,1" : :     怕誤會補充一下:如果 aa 變成10_11_12 : :     ac 就會變成 1,0_1,1_1,2 : input: "1_23_456_7890" : output: "1_2,3_4,5,6_7,8,9,0" 直接處理原生物件最快,不需要用String.split /** * @param input "1_23_456_7890" * @return "1_2,3_4,5,6_7,8,9,0" */ private static String convert(String input){ final StringBuilder sb=new StringBuilder(); boolean skip=true; //determine whether to skip ',' for(final char c:input.toCharArray()){ if('_'==c)skip=true; else if(skip)skip=false; else sb.append(','); sb.append(c); } return sb.toString(); } public static void main(String[] args) { System.out.println(convert("1_23_456_7890")); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.176.205.39 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1421084186.A.E74.html
文章代碼(AID): #1Kj0OQvq (java)
討論串 (同標題文章)
文章代碼(AID): #1Kj0OQvq (java)