final int mid = s1.length() / 2; //get the middle of the String
String[] parts = {s1.substring(0, mid),s1.substring(mid)};
System.out.println(parts[0]); //first part
System.out.println(parts[1]); //second part
Answer from karim mohsen on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › split a string in java
Split a String in Java | Baeldung
January 8, 2024 - Here’s an example that splits a string in half: String hello = "Baeldung"; int mid = hello.length() / 2; String[] parts = { hello.substring(0, mid), hello.substring(mid) }; Here, we calculate the midpoint of the string value.
Top answer
1 of 3
2

You can do it for example like this:

String base = "somestring";
int half = base.length() % 2 == 0 ? base.length()/2 : base.length()/2 + 1;
String first = base.substring(0, half);
String second = base.substring(half);

Simply when n is the string's length, if n is divisible by 2, split the string in n/2, otherwise split in n/2 + 1 so that first substring is one character longer than second.

2 of 3
0

What do you do to divide an odd number e.g. 15 with the same requirement?

You store the result of 15 / 2 into an int variable say

int half = 15 / 2 

which gives you 7. As per your requirement, you need to add 1 to half to make the first half (i.e. 8) and the remaining half will be 15 - 8 = 7.

On the other hand, in case of an even number, you simply divide it by 2 to have two halves.

You have to apply the same logic in the case of a String as well. Given below is a demo:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int half;
        String str1 = "Titanic";
        half = str1.length() / 2;
        String str1Part1 = str1.substring(0, half + 1);
        String str1Part2 = str1.substring(half + 1);
        System.out.println(str1Part1 + ", " + str1Part2);

        String str2 = "HelloWorld";
        half = str2.length() / 2;
        String str2Part1 = str2.substring(0, half);
        String str2Part2 = str2.substring(half);
        System.out.println(str2Part1 + ", " + str2Part2);

        Scanner in = new Scanner(System.in);
        do {
            System.out.print("Enter a string: ");
            String str = in.nextLine();
            half = str.length() / 2;
            System.out.println(str.length() % 2 == 1 ? str.substring(0, half + 1) + ", " + str.substring(half + 1)
                    : str.substring(0, half) + ", " + str.substring(half));
            System.out.print("Enter Y to continue or any input to exit: ");
        } while (in.nextLine().toUpperCase().equals("Y"));
    }
}

A sample run:

Tita, nic
Hello, World
Enter a string: Arvind
Arv, ind
Would you like to continue? [Y/N]: y
Enter a string: Kumar
Kum, ar
Would you like to continue? [Y/N]: Y
Enter a string: Avinash
Avin, ash
Would you like to continue? [Y/N]: n

Note:

  1. % is a modulo operator.
  2. Check String substring​(int beginIndex, int endIndex) and String substring​(int beginIndex) to learn more about substring functions of String.
  3. Check https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html to learn about the ternary operator.
🌐
W3Schools
w3schools.com › java › ref_string_split.asp
Java String split() Method
The split() method splits a string into an array of substrings using a regular expression as the separator. If a limit is specified, the returned array will not be longer than the limit.
🌐
IONOS
ionos.com › digital guide › websites › web development › string splitting in java
How to split strings in Java - IONOS
January 6, 2025 - The method split() can be used to split strings in Java. It contains a parameter for the separator and an optional parameter for the number of sub­strings. There are also some par­tic­u­lar­i­ties to note when using the method.
🌐
Medium
medium.com › @AlexanderObregon › splitting-a-word-into-two-parts-in-java-strings-c9d1aff469ac
Splitting a Word into Two Parts in Java Strings | Medium
August 22, 2025 - The substring method is the central tool for cutting a word into parts. It works by creating a new string from a specified range of characters, without altering the original. If a word is "Computer", choosing index 4 produces "Comp" as the first ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › split-string-java-examples
Java String split() Method - GeeksforGeeks
May 13, 2026 - The split() method in Java is used to divide a string into multiple parts based on a specified delimiter (regular expression). It returns an array of substrings after splitting the original string.
🌐
Stack Abuse
stackabuse.com › how-to-split-a-string-in-java
How to Split a String in Java
September 20, 2023 - In this article, we'll take a look at how we can split a string in Java. We will also explore different examples and common issues with using the split() method.
Find elsewhere
Top answer
1 of 2
21

There's no obvious regex pattern that would do this. It may be possible to do this with String.split, but I'd just use substring like this:

    String s = "12345678abcdefgh";

    final int mid = s.length() / 2;
    String[] parts = {
        s.substring(0, mid),
        s.substring(mid),
    };

    System.out.println(Arrays.toString(parts)); 
    // "[12345678, abcdefgh]"

The above would split an odd-length String with part[1] one character longer than part[0]. If you need it the other way around, then simply define mid = (s.length() + 1) / 2;


N-part split

You can also do something like this to split a string into N-parts:

static String[] splitN(String s, final int N) {
    final int base = s.length() / N;
    final int remainder = s.length() % N;

    String[] parts = new String[N];
    for (int i = 0; i < N; i++) {
        int length = base + (i < remainder ? 1 : 0);
        parts[i] = s.substring(0, length);
        s = s.substring(length);
    }
    return parts;
}

Then you can do:

    String s = "123456789";

    System.out.println(Arrays.toString(splitN(s, 2)));  
    // "[12345, 6789]"

    System.out.println(Arrays.toString(splitN(s, 3)));
    // "[123, 456, 789]"

    System.out.println(Arrays.toString(splitN(s, 5)));  
    // "[12, 34, 56, 78, 9]"

    System.out.println(Arrays.toString(splitN(s, 10))); 
    // "[1, 2, 3, 4, 5, 6, 7, 8, 9, ]"

Note that this favors the earlier parts to hold the extra characters, and it also works when the number of parts is more than the number of characters.


Appendix

In the above code:

  • ?: is the conditional operator, aka the ternary operator.
  • / performs integer division. 1 / 2 == 0.
  • % performs integer remainder operation. 3 % 2 == 1. Also, -1 % 2 == -1.

References

  • JLS 15.25 Conditional Operator ?:
  • JLS 15.17.2 Division Operator /
  • JLS 15.17.3 Remainder Operator %

Related questions

  • How does the ternary operator work?
  • Why does (360 / 24) / 60 = 0 … in Java
2 of 2
8

You really don't need a regex for this. Just use substring().

int midpoint = str.length() / 2;
String firstHalf = str.substring(0, midpoint);
String secondHalf = str.substring(midpoint);
🌐
W3Docs
w3docs.com › java
How to Split a String in Java | Practice with examples | W3Docs
Learn the ways to split a string in Java. The most common way is using the String.split () method, also see how to use StringTokenizer and Pattern.compile ().
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string split() method – how to split a string in java
Java String Split() Method – How To Split A String In Java
April 1, 2025 - Using the Java Split() method, we will successfully print each of the words without including the space. Explanation: Here, we have initialized a Java String variable and using the regular expression “\\s”, we have split the String wherever whitespace occurred.
🌐
Sentry
sentry.io › sentry answers › java › how to split a string in java
How to split a string in Java | Sentry
May 15, 2023 - The easiest way to split a string in Java is to use the String.split() method.
🌐
LightNode
go.lightnode.com › tech › java-string-split
Mastering Java String Split: Essential Techniques for Efficient Text Processing
April 15, 2025 - String manipulation can be resource-intensive, especially with large texts or frequent operations. Here are some techniques to optimize your code: When you need to apply the same split operation multiple times, using a pre-compiled Pattern object can improve performance: import java.util.regex.Pattern; // Pre-compile the pattern Pattern pattern = Pattern.compile(","); // Use it multiple times String[] fruits1 = pattern.split("apple,banana,orange"); String[] fruits2 = pattern.split("pear,grape,melon");
🌐
Guru99
guru99.com › home › java tutorials › split() string method in java: how to split string with example
Split() String Method in Java: How to Split String with Example
December 26, 2023 - StrSplit() method allows you to break a string based on specific Java string delimiter. Mostly the Java string split attribute will be a space or a comma(,) with which you want to break or split the string
🌐
Codecademy
codecademy.com › docs › java › strings › .split()
Java | Strings | .split() | Codecademy
March 30, 2022 - Splits a string into an array of substrings based on a delimiter pattern.
🌐
Baeldung
baeldung.com › home › java › java string › java string.split()
Java.String.split() | Baeldung
November 9, 2017 - We can also pass a limit on the number of splits to the split() method. The limit determines how many times the string will be split: If the limit is greater than 0, the string is split at most limit – 1 times.
🌐
Studytonight
studytonight.com › java-examples › java-string-split-method
Java String Split() Method - Studytonight
This tutorial explains how to use the split method(split()) to split a string into an array of strings by using regular expressions or delimiters.