split()函數是根據參數如",", "-", " "等, 分割String字符串, 返回一個String的數組(String[]), 可以通過索引指定元素.
如果未找到, 則返回整個String字符串, 作為String數組(String[])的第0個元素.
trim()函數是去掉String字符串的首尾空格;
代碼:
/** * @author Spike * @time 2014.4.25 */ public class Split { public static void split() { String details = "M 2.9 - 9km W of Alberto Oviedo Mota, Mexico"; String magnitudeString = details.split(" ")[1]; System.out.println(magnitudeString); details = details.split(",")[1].trim(); System.out.println(details); } public static void main(String[] args) { // TODO Auto-generated method stub split(); } }
輸出:
2.9 Mexico
作者:csdn博客 Spike_King