如下所示:
public class TestStringToken {
@Test
public void subSting() {
String str = "java,javac,javae";
String s1 = str.substring(2);//"va,javae"
String s2 = str.substring(0,2);//"ja"
System.out.println(s1);
System.out.println(s2);
}
@Test
public void stringTokenizer() {
String str = "java,javae";
StringTokenizer stringTokenizer = new StringTokenizer(str,",");
// while (stringTokenizer.hasMoreElements()) {
// Object object = (Object) stringTokenizer.nextElement();
// System.out.println(String.valueOf(object));
//
// }
while (stringTokenizer.hasMoreTokens()) {
System.out.println(stringTokenizer.nextToken());
}
}
@Test
public void split() {
String str = "java,javae";
String[] s = str.split(",");
for (String string : s) {
System.out.println(string);
}
}
//使用正则表达式
@Test
public void pattern() {
String str = "java,javae";
Pattern p = Pattern.compile(",");
String[] s = p.split(str);
for (String string : s) {
System.out.println(string);
}
}
}
以上这篇String字符串截取的四种方式总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。
原文链接:https://www.f2er.com/js/44120.html