Split a string with delimiter hyphenString str = "how to do-in-java-provides-java-tutorials"; String[] strArray = str.split("-"); //[how to do, in, java, provides, java, tutorials] 2.2. 通过空格进行分割 以下的Java程序使用分隔符
To split a string in Java using a dot (.) as the delimiter, you can use the split method of the String class. Here is an example of how to do this: String input = "this.is.a.test"; String[] parts = input.split("\\."); Copy This will split the input string into an ...
In this post, we will see how to split a String by delimiter in java. Sometimes, we need to split a String by delimiter for example: while reading a csv file, we need to split string by comma(,). We will use String class’s split method to split a String. This split(regex) ...
publicclassJavaExample{publicstaticvoidmain(String[]args){//A string with multiple delimitersStringstr="Text1,Text2#Text3 Text4:Text5.Text6";//Specified the delimiters inside brackets, there is a//whitespace after # to consider space as delimiter as wellString[]strArray=str.split("[,# :.]...
String[] parts = string.split("(?<=-)"); String part1 = parts[0]; // 004- String part2 = parts[1]; // 034556 1. 2. 3. 4. 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. ...
1. Using Plain Java TheString.split()method is the best and recommended way to split the strings. The tokens are returned in form of astring arraythat frees us to use it as we wish. The following Java program splits a string with the delimitercomma. It is equivalent to splitting a CSV...
Example 1: Split string using word as delimiter Here, a string (a word) is used as a delimiter insplit()method. publicclassJavaExample{publicstaticvoidmain(Stringargs[]){Stringstr="helloxyzhixyzbye";String[]arr=str.split("xyz");for(Strings:arr)System.out.println(s);}} ...
Split a string by space Stringstr="how to do injava";String[]strArray=str.split("\\s");//[how, to, to, injava] 2.3. Split by Comma Java program to split a string by delimitercomma. Split a string with a comma Stringstr="A,B,C,D";String[]strArray=str.split(",");//[A,...
Now I'll split it using delimiter as \ . So it will become C: check out checking out BACKUPER "check out" and "checking out" contain space.The code will remove the space and convert them into "checkout" and "checkingout". Then truncate them into strings of 6 characters an...
publicclassMain {publicstaticvoidmain(String[] args) {Stringstr ="AL,FL,NY,CA,GA";// Split str using a comma as the delimiterString[] parts = str.split(",");// Print the the string and its partsSystem.out.println(str);for(Stringpart : parts) {System.out.println(part); ...