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

JAVA - split: 将字符串分割成数组_wutangpuer的博客-CSDN博客_ ...

https://blog.csdn.net/wutangpuer/article/details/41442525

2014年11月24日 ... 利用split字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string. split的用法实例及注意事项:java.lang.string.splitsplit 方法将 ...

How to split a string in Java - Stack Overflow

https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java

which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\.") , or use character class [] ...

Java split() 方法| 菜鸟教程

https://www.runoob.com/java/java-string-split.html

Java split() 方法Java String类split() 方法根据匹配给定的正则表达式来拆分字符串 。 注意: . 、 $、 | 和* 等转义字符,必须得加\\。 注意:多个分隔符,可以用| ...

Java字符串split() 的使用方法.没你想的那么简单_请叫我大师兄 ...

https://blog.csdn.net/qq_27093465/article/details/54910323

2017年2月7日 ... 先看下面的方法,事先预测一下,经过split方法,按逗号进行分割为数组之后, 生成的数组的长度是多少,目测很多人都觉得是8,但是结果却出乎 ...

Split() String method in Java with examples - GeeksforGeeks

https://www.geeksforgeeks.org/split-string-java-examples/

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 ...

[Java] String.split()的用法- 知乎

https://zhuanlan.zhihu.com/p/45151144

2018年9月22日 ... 本文讨论Java中的split函数.split()是一个用来切分字符串的函数, 相信大家都用过, 但是, 可能他不是你想象中的那么简单. 解析public String[] ...

Java 字符串split 踩坑记- SegmentFault 思否

https://segmentfault.com/a/1190000020684753

2019年10月14日 ... 前几天在公司对通过FTP 方式上传的数据文件按照事先规定的格式进行解析后入库 ,代码的大概实现思路是这样的:先使用流进行文件读取, ...

Javasplit函数进行分割字符串- 潭州学院的个人空间- OSCHINA ...

https://my.oschina.net/u/2367675/blog/420735

2015年5月27日 ...java中,经常会对字符串进行分割,使用split方法把字符串按照指定的分割符进行 分割,然后返回字符串数组,下面是string.split的用法实例及注意 ...

String (Java Platform SE 7 )

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

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 ...

Split() String Method in Java: How to Split String with Example

https://www.guru99.com/how-to-split-a-string-in-java.html

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 ...

字符串分割--java中String.split()用法- 华为云

https://www.huaweicloud.com/articles/b754fc215ff10a785313a3e202bc1b20.html

2021年2月7日 ...java.lang包中有String.split()方法,返回是一个数组。 1、“.”和“|”都是转义 ... 3、 public String[] split(String regex,int limit)根据匹配给定的正则表达式来拆分此字符 。 ... 我们看到了结果是以一个指定的字符进行的分割。使用另一 ...

Javasplit函数进行分割字符串-JAVA学习视频教程-51CTO博客

https://blog.51cto.com/10239772/1655759

2015年5月27日 ...java中,我们常常需要对字符串进行分割,使用split方法把字符串按照指定的分割符 进行分割,然后返回字符串数组,下面是string.split的用法实例 ...

寻找Java中String.split性能更好的方法- SegmentFault 思否

https://segmentfault.com/a/1190000016901608

2018年11月4日 ... String.split 方法的分割参数 regex 实际不是字符串,而是正则表达式,就是说分隔 字符串支持按正则进行分割,虽然这个特性看上去非常好,但从 ...

Java String split() method - javatpoint

https://www.javatpoint.com/java-string-split

Java String split() method with regex and length example · public class SplitExample2{ · public static void main(String args[]){ · String s1="welcome to split world"; ...

Java String split() Method with examples

https://beginnersbook.com/2013/12/java-string-split-method-example/

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 ...

How to Split a String in Java | Practice with examples

https://www.w3docs.com/snippets/java/how-to-split-a-string-in-java.html

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 - Tutorialspoint

https://www.tutorialspoint.com/java/java_string_split.htm

Java - String split() Method - This method has two variants and splits this string around matches of the given regular expression.

How to split String in Java by WhiteSpace or tabs ... - Javarevisited

https://javarevisited.blogspot.com/2016/10/how-to-split-string-in-java-by-whitespace-or-tabs.html

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 ...

Java.String.split() | Baeldung

https://www.baeldung.com/string/split

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 ...