java.lang.String.split()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.2k)|赞(0)|评价(0)|浏览(237)

本文整理了Java中java.lang.String.split()方法的一些代码示例,展示了String.split()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。String.split()方法的具体详情如下:
包路径:java.lang.String
类名称:String
方法名:split

String.split介绍

[英]Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument #split(String,int) method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The string "boo:and:foo", for example, yields the following results with these expressions:
RegexResult:{ "boo", "and", "foo" }o{ "b", "", ":and:f" }
[中]在给定regular expression的匹配项周围拆分此字符串。
该方法的工作原理类似于使用给定表达式和零限制参数调用双参数#split(String,int)方法。因此,结果数组中不包括尾随的空字符串。
例如,字符串“boo:and:foo”使用以下表达式生成以下结果:
RegexResult:{“boo”,“and”,“foo”}o{“b”、“”、“:和:f”}

代码示例

代码示例来源:origin: stackoverflow.com

String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556

代码示例来源:origin: stackoverflow.com

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

代码示例来源:origin: stackoverflow.com

String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556

代码示例来源:origin: stackoverflow.com

String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42

代码示例来源:origin: stackoverflow.com

String contentType = connection.getHeaderField("Content-Type");
String charset = null;

for (String param : contentType.replace(" ", "").split(";")) {
  if (param.startsWith("charset=")) {
    charset = param.split("=", 2)[1];
    break;
  }
}

if (charset != null) {
  try (BufferedReader reader = new BufferedReader(new InputStreamReader(response, charset))) {
    for (String line; (line = reader.readLine()) != null;) {
      // ... System.out.println(line) ?
    }
  }
} else {
  // It's likely binary content, use InputStream/OutputStream.
}

代码示例来源:origin: stackoverflow.com

// Gather all cookies on the first request.
URLConnection connection = new URL(url).openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...

// Then use the same cookies on all subsequent requests.
connection = new URL(url).openConnection();
for (String cookie : cookies) {
  connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// ...

代码示例来源:origin: spring-projects/spring-framework

@Override
public MyCustomElement unmarshal(String c) throws Exception {
  String[] t = c.split("\\|\\|\\|");
  return new MyCustomElement(t[0], t[1]);
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Flowable<String> apply(Resource resource) {
    return Flowable.fromArray(resource.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Flowable<String> apply(Resource res) {
    return Flowable.fromArray(res.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Flowable<String> apply(Resource resource) {
    return Flowable.fromArray(resource.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<String> apply(Resource res) {
      return Observable.fromArray(res.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<String> apply(Resource resource) {
    return Observable.fromArray(resource.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Flowable<String> apply(Resource res) {
      return Flowable.fromArray(res.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<String> apply(Resource res) {
    return Observable.fromArray(res.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<String> apply(Resource resource) {
    return Observable.fromArray(resource.getTextFromWeb().split(" "));
  }
};

代码示例来源:origin: google/guava

public void testComputeIfPresent() {
 cache.put(key, "1");
 // simultaneous update for same key, expect count successful updates
 doParallelCacheOp(
   count,
   n -> {
    cache.asMap().computeIfPresent(key, (k, v) -> v + delimiter + n);
   });
 assertEquals(1, cache.size());
 assertThat(cache.getIfPresent(key).split(delimiter)).hasLength(count + 1);
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<String> apply(Resource resource) {
    return Observable.fromArray(resource.getTextFromWeb().split(" "))
        .concatWith(Observable.<String>error(new RuntimeException()));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Flowable<String> apply(Resource resource) {
    return Flowable.fromArray(resource.getTextFromWeb().split(" "))
        .concatWith(Flowable.<String>error(new RuntimeException()));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<String> apply(Resource resource) {
    return Observable.fromArray(resource.getTextFromWeb().split(" "))
        .concatWith(Observable.<String>error(new RuntimeException()));
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Flowable<String> apply(Resource resource) {
    return Flowable.fromArray(resource.getTextFromWeb().split(" "))
        .concatWith(Flowable.<String>error(new RuntimeException()));
  }
};

相关文章