import org.apache.commons.lang.StringUtils; public class substr{ public static void main(String[] args) { String str = "1234567890ABCDEFG"; System.out.println("-----------" + str.substring(0)); //从字符串索引为0开始截取,一直到字符串末尾 System.out.println("-----1------" + str.substring(6,7)); //从索引6开始到索引7结束(不包含索引7,实际截取的是"7") System.out.println("-----2------" + str.substring(str.length()-3, str.length()-1)); System.out.println("-----3------" + str.substring(0, str.indexOf("4"))); System.out.println("-----4------" + str.substring(str.indexOf("4") + 1, str.length())); System.out.println("-----5------" + str.substring(str.indexOf("4"), str.indexOf("F"))); String strs = "0899991003535197"; System.out.println("===========" + strs.substring(6, strs.length())); System.out.println("=====1======" + strs.substring(0, 2)); System.out.println("=====2======" + StringUtils.substringBefore(strs, "3")); System.out.println("=====3======" + StringUtils.substringBeforeLast(strs, "3")); System.out.println("=====4======" + StringUtils.substringAfter(strs, "3")); System.out.println("=====5======" + StringUtils.substringAfterLast(strs, "3")); String str1 = "as,asd,df,asd,efd,asd"; String [] list1 = str1.split(","); for(int i=0;i<list1.length;i++) { System.out.println("第"+(i+1)+"个元素为:"+list1[i]); } } }
运行结果:
-----------1234567890ABCDEFG
-----1------7
-----2------EF
-----3------123
-----4------567890ABCDEFG
-----5------4567890ABCDE
===========1003535197
=====1======08
=====2======089999100
=====3======08999910035
=====4======535197
=====5======5197
第1个元素为:as
第2个元素为:asd
第3个元素为:df
第4个元素为:asd
第5个元素为:efd
第6个元素为:asd