java字符串截取
程序员文章站
2022-06-20 18:33:03
import org.apache.commons.lang.StringUtils; public class substr{ public static void main(String[] args) { String str = "1234567890ABCDEFG"; System.out ......
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