Just use the appropriate method: String#split()
.
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
Note that this takes a regular expression, so remember to escape special characters if necessary.
there are 12 characters with special meanings: the backslash
\
, the caret^
, the dollar sign$
, the period or dot.
, the vertical bar or pipe symbol|
, the question mark?
, the asterisk or star*
, the plus sign+
, the opening parenthesis(
, the closing parenthesis)
, and the opening square bracket[
, the opening curly brace{
, These special characters are often called "metacharacters".
So, if you want to split on e.g. period/dot .
which means "any character" in regex, use either backslash \
to escape the individual special character like so split("\\.")
, or use character class []
to represent literal character(s) like so split("[.]")
, or use Pattern#quote()
to escape the entire string like so split(Pattern.quote("."))
.
String[] parts = string.split(Pattern.quote(".")); // Split on period.
To test beforehand if the string contains certain character(s), just use String#contains()
.
if (string.contains("-")) {
// Split it.
} else {
throw new IllegalArgumentException("String " + string + " does not contain -");
}
Note, this does not take a regular expression. For that, use String#matches()
instead.
If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<=
group on the pattern.
String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556
In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?=
group on the pattern.
String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556
If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split()
method.
String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42
2014年11月24日 ... 利用split把字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string. split的用法实例及注意事项:java.lang.string.splitsplit 方法将 ...
which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\.") , or use character class [] ...
Java split() 方法Java String类split() 方法根据匹配给定的正则表达式来拆分字符串 。 注意: . 、 $、 | 和* 等转义字符,必须得加\\。 注意:多个分隔符,可以用| ...
2017年2月7日 ... 先看下面的方法,事先预测一下,经过split方法,按逗号进行分割为数组之后, 生成的数组的长度是多少,目测很多人都觉得是8,但是结果却出乎 ...
Dec 4, 2018 ... The string split() method breaks a given string around matches of the given regular expression. For Example: Input String: 016-78967 Regular ...
2018年9月22日 ... 本文讨论Java中的split函数.split()是一个用来切分字符串的函数, 相信大家都用过, 但是, 可能他不是你想象中的那么简单. 解析public String[] ...
2019年10月14日 ... 前几天在公司对通过FTP 方式上传的数据文件按照事先规定的格式进行解析后入库 ,代码的大概实现思路是这样的:先使用流进行文件读取, ...
2015年5月27日 ... 在java中,经常会对字符串进行分割,使用split方法把字符串按照指定的分割符进行 分割,然后返回字符串数组,下面是string.split的用法实例及注意 ...
All string literals in Java programs, such as "abc" , are implemented as ... An invocation of this method of the form str.split(regex, n) yields the same result as the ...
Regex: The regular expression in Java split is applied to the text/string · Limit: A limit in Java string split is a maximum number of values in the array. If it is omitted or ...
2021年2月7日 ... 在java.lang包中有String.split()方法,返回是一个数组。 1、“.”和“|”都是转义 ... 3、 public String[] split(String regex,int limit)根据匹配给定的正则表达式来拆分此字符 串。 ... 我们看到了结果是以一个指定的字符进行的分割。使用另一 ...
2015年5月27日 ... 在java中,我们常常需要对字符串进行分割,使用split方法把字符串按照指定的分割符 进行分割,然后返回字符串数组,下面是string.split的用法实例 ...
2018年11月4日 ... String.split 方法的分割参数 regex 实际不是字符串,而是正则表达式,就是说分隔 字符串支持按正则进行分割,虽然这个特性看上去非常好,但从 ...
Java String split() method with regex and length example · public class SplitExample2{ · public static void main(String args[]){ · String s1="welcome to split world"; ...
Dec 28, 2013 ... Java String Split Method. We have two variants of split() method in String class. 1. String[] split(String regex) : It returns an ...
There are many ways to split a string in Java. The most common way is using the split() method which is used to split a string into an array of sub-strings and ...
Java - String split() Method - This method has two variants and splits this string around matches of the given regular expression.
Oct 7, 2016 ... This is the simplest case. Usually, words are separated by just one white space between them. In order to split it and get the array of words, just ...
Apr 15, 2021 ... The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the ...