Home > php教程 > PHP开发 > Summary of four ways to intercept String strings

Summary of four ways to intercept String strings

高洛峰
Release: 2016-12-03 17:00:07
Original
1849 people have browsed it

Looks like this:

import java.util.StringTokenizer;
import java.util.regex.Pattern;
 
import org.junit.Test;
 
public class TestStringToken {
 
  @Test
  public void subSting() {
    String str = "java,javac,javae";
    String s1 = str.substring(2);//"va,javac,javae"
    String s2 = str.substring(0,2);//"ja"
    System.out.println(s1);
    System.out.println(s2);
  }
 
  @Test
  public void stringTokenizer() {
    String str = "java,javac,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,javac,javae";
    String[] s = str.split(",");
    for (String string : s) {
      System.out.println(string);
    }
  }
  //使用正则表达式
  @Test
  public void pattern() {
    String str = "java,javac,javae";
    Pattern p = Pattern.compile(",");
    String[] s = p.split(str);
    for (String string : s) {
      System.out.println(string);
    }
  }
 
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template