看板 java 關於我們 聯絡資訊
※ 引述《scott260202 (Cake)》之銘言: : 我有讀入一整行的String 一整行喔 : 所以會有很多空白字元 : 例如 : " a 123 b \n" : 以上字串已經存在一個String裡面了 : 有什麼指令或方法可以抓出 那個'a' : // 因為必須忽略後面的東西 且下次讀東西的時候不能讀到他們 : 所以不能用跳過空白的方式讀一個小字串 : 請各位高手幫個忙吧 感恩^.= 看到回文使用 replaceAll 將空白字元置換, 但是WhiteSpace其實包含了[ \t\n\x0B\f\r], 我會建議使用 regular explression 給您個sample , 具體用法請參考API Doc Pattern p = Pattern.compile("\\w"); String msg = " \t \n \u000B \f \r a 123 b \n"; Matcher m = p.matcher(msg); if(m.find()) System.out.println(String.format("find %s at %d position", msg.charAt(m.start()),m.start())); 輸出結果為 find a at 15 position -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.20.140.43
ogamenewbie:msg.trim().charAt(0) 的結果不對嘛? @_@a 06/10 16:49
kentyeh:msg.trim().charAt(0)可以,除非msg不全由whitespace組成 06/10 20:50