看板 java 關於我們 聯絡資訊
※ 引述《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