split()函数和trim()函数的使用方法
本文地址: http://blog.csdn.net/caroline_wendy/article/details/24465141
详细參考Java API: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
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