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