Use Arrays.copyOfRange:

public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)

Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

The resulting array is of exactly the same class as the original array.

In your case:

String[] grp = Arrays.copyOfRange(elements, i, i + n);
Answer from NPE on Stack Overflow
🌐
CodeGym
codegym.cc › java blog › strings in java › substring in java
Java String substring()
String x = "CodeGymIsTheBest"; String y = x.substring (2,6); String z = x.substring (0,3); So, in JDK 7 and later, objects y and z created as a result of the substring() method applied to object x will refer to two newly created arrays (on the heap) - {d,e, G, y} for y and {C, o} for z. In JDK 7 + version of the method substring, these two new lines (that is, two new character arrays) will be stored in memory along with the original string myLongString ({C, o, d, e, G, y, m, i, s, t, h, e, b,e,s,t} in the form of an array). Even today on some big projects you may meet legacy code base from the JDK 6 times. In JDK 6 method substring() works in a different way. As you probably know, String is an immutable Class and to get the substring Java used this immutability earlier in JDK 6.
Published: July 23, 2024
Discussions

Getting a substring of an array in java - Stack Overflow
I want to go through an int[] array and get substring values from it. More on stackoverflow.com
🌐 stackoverflow.com
Array and .substring in java - Stack Overflow
Seems like a job for Arrays.sort... ... What does substring() have to do with this? The method is for extracting part of a string. More on stackoverflow.com
🌐 stackoverflow.com
December 9, 2018
Substring into an array in Java - Stack Overflow
12252 How can I remove a specific item from an array in JavaScript? 7403 How to check whether a string contains a substring in JavaScript? More on stackoverflow.com
🌐 stackoverflow.com
Java manipulating substring/char in a string array - Stack Overflow
I have a string array in JAVA like: String[] fruits = {"banana", "apple", "orange"}; How can I access to a character/substring of a string member? For example I want to show the second character of More on stackoverflow.com
🌐 stackoverflow.com
🌐
HappyCoders
happycoders.eu › home › java › java substring() method
Java substring() Method
June 12, 2025 - If the original string is no longer ... its char array because the substring still references it. For example, if the original string contains 10,000 characters and the substring contains only ten characters, then 9,990 characters, or just under 20 KB (one char occupies two bytes) of the heap would be wasted. Java developers ...
🌐
Stack Overflow
stackoverflow.com › questions › 29382983 › getting-a-substring-of-an-array-in-java
Getting a substring of an array in java - Stack Overflow
I know for strings you have substring(inclusive, exclusive) is there anything similar for an array or do I have to convert the array to a string in order to do such a thing? ... what do you mean by "reverse" order? why comparing {1, 2, 1} with {4, 1, 2} and how? Please share more examples with clear requirement and understanding. ... java.util.Arrays.copyOfRange (specialized for each primitive element type or Object) gives a portion, in the same order, like String.substring.
🌐
TutorialsPoint
tutorialspoint.com › extracting-a-substring-as-an-array-of-characters-in-java
Java - String getChars() Method
Using the getChars() method, we are trying to copy the string characters into the char array at the specified srcIndex 5, srcEnd 15, and dstbegin 0. package com.tutorialspoint; import java.util.Arrays; public class GetChars { public static void main(String[] args) { //instantiate the string class String str = new String("Welcome to TutorialsPoint"); System.out.println("The given string is: " + str); // create character array char[] chararr = new char[10]; // copies characters from starting and ending index into the destination character array //initialize the srcBegin, srcEnd, and dstBegin val
🌐
W3Schools
w3schools.com › java › ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The substring() method returns a substring from the string.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 ) - Oracle Help Center
July 21, 2026 - If the expression does not match any part of the input then the resulting array has just one element, namely this string. When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array.
🌐
LinkedIn
linkedin.com › pulse › working-java-substring-6-jyoti-jindal
Implementation of Java substring() in Java 6, and why it was changed in Java 7?
February 14, 2023 - When we take substring from original string, new String object will be created in constant pool or in heap. The value[] char array will be shared among two String objects, but count and offset attributes of String object will vary according to substring length and starting index.
🌐
Coderanch
coderanch.com › t › 486735 › java › fast-substring-search-Array-strings
How to do a fast substring search on an Array of strings? (Java in General forum at Coderanch)
March 11, 2010 - First ".*?" means shy matching (it doesn't tries to possess the whole string and then backtrack - instead it tries to match the next expression after each character match). So could you please try testing match of the this example? Next, indexOf() performs only search of the substring.
🌐
Baeldung
baeldung.com › home › java › java string › get substring from string in java
Get Substring from String in Java | Baeldung
July 21, 2024 - For more details on the Java regular expressions check out this tutorial. We can use the split method from the String class to extract a substring. Say we want to extract the first sentence from the example String. This is quite easy to do using split: ... Since the split method accepts a regex we had to escape the period character. Now the result is an array of 2 sentences.
🌐
Stack Overflow
stackoverflow.com › questions › 70026085 › substring-into-an-array-in-java
Substring into an array in Java - Stack Overflow
.substring() does not return an array, it returns a String so passing it into an array won't work. You can split a String into a String[] array using the .split(String regex) method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - Explanation: In the above code example, we use the substring(int beginIndex) method and specify the start index which is 8 and it didn't specified the end index. So it will make substring from end of the given string and we get the substring as "Geeks". ... Example 2: Java program to extract a substring from specified start index to specified end index using substring(int beginIndex, int endIndex).
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › substring in java
Substring in Java – Syntax, Examples & Usage Explained
May 14, 2025 - Yes, substring can be chained with ... and makes string manipulation concise and expressive in Java. substring() extracts a part of the string based on index positions. In contrast, split() breaks a string into an array using a delimiter like ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › String.html
String (Java Platform SE 7 )
A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m+1). This method may be used to trim whitespace (as defined above) from the beginning and end ...
🌐
Blogger
javarevisited.blogspot.com › 2011 › 10 › how-substring-in-java-works.html
How SubString method works in Java - Memory Leak Fixed in JDK 1.7
In case If you have still not figured it out, If the original string is very long, and has array of size 1GB, no matter how small a substring is, it will hold 1GB array. This will also stop original string to be garbage collected, in case if doesn't have any live reference. This is a clear case of memory leak in Java, where memory is retained even if it's not required.
🌐
DevGenius
blog.devgenius.io › java-substring-a-comprehensive-guide-082192995d1e
Java Substring Failed When Input Changed Shape | Dev Genius
July 6, 2026 - A substring is a sequence of characters derived from another string. For example, in the string "Hello, World!", the substrings "Hello", "World", and "lo, Wo" are all valid substrings.
🌐
Hacker News
news.ycombinator.com › item
Substrings in older versions of Java used to reuse the character array of the or... | Hacker News
September 17, 2020 - Go, which is garbage-collected, also does the slice-by-reference trick (but fails criterion #1 because it doesn't really have iterators)
🌐
Educative
educative.io › blog › how-do-you-master-java-strings-and-arrays-for-beginners
How do you master Java strings and arrays for beginners
Substring extraction: The substring() method has two variants, one taking a start index only and another taking both start and end indexes, where the end index character is excluded from the result. Array initialization and operations: Arrays can be initialized with default values, after declaration using the new keyword, or inline with assigned values, and Java's Arrays.sort() and Arrays.binarySearch() handle sorting and searching efficiently.