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 Overflowfinal 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
You can use Java's substring function to divide them in halves:
String s1a = s1.substring(0, (s1.length()/2));
String s1b = s1.substring(s1.length()/2);
Videos
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.
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:
%is a modulo operator.- Check String substring(int beginIndex, int endIndex) and String substring(int beginIndex) to learn more about
substringfunctions ofString. - Check https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html to learn about the ternary operator.
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
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);
To make it dynamically, do it based on array length and split it in two:
int [] x= {8,7,5,6,2,4,3,1};
int len = x.length;
int a[] = Arrays.copyOfRange(mergeSort(x), 0, len/2);
int b[] = Arrays.copyOfRange(mergeSort(x), (len/2), len);
System.out.println("A: " + Arrays.toString(a));
System.out.println("B: " + Arrays.toString(b));
Hope it helps.
An edit to the accepted answer could take into account that half an odd numbered int is actually the nearest, smaller, whole number, to the half. So, something like this is more appropriate:
int size = array.size();
int half = size % 2 == 0 ? size / 2 : (size / 2) + 1;