可以使用两种方法 substring和split
- package test;
- public class substringTest
- {
- public static void main(String args[])
- {
- String N = "字符串";
- String L="";
- String R="";
- int k= N.length();
- for (int i = 0; i < N.length(); i++)
- {
- if (N.substring(i, i + 1).equals("|"))
- {
- L=N.substring(0,i).trim();
- R=N.substring(i+1,k).trim();
- }
- else
- {
- }
- System.out.println(L);
- System.out.println(R);
- }
- }
- }
或者split:
- public class Test01 {
- public static void main(String[] args) {
- String str = "onetwo";
- String regex = "(?<=one)(?=two)";
- String[] strs = str.split(regex);
- for(int i = 0; i < strs.length; i++) {
- System.out.printf("strs[%d] = %s%n", i, strs[i]);
- }
- }
- }