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.
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).
What does String.substring exactly do in Java? - Stack Overflow
I always thought if I do String s = "Hello World".substring(0, 5), then I just get a new string s = "Hello". This is also documented in the Java API doc: "Returns a new string that is a substring o... More on stackoverflow.com
String.format() is 3x faster in Java 17
Glad to see some of the small enhancements we did in 17 get recognition. Not sure if this one matters, but String::format shows up in profiles every now and then so it felt reasonable to me to give it some TLC in between larger projects. More on reddit.com
How to correctly use the substring method? (Java)
You are deeply confused about how substring() works - it's a method of String, and it does not take Strings as parameters. See here for an example. More on reddit.com
Why does the substring method in the String class create a new object and return that memory address rather than the information itself or interning it into the String Pool and returning that memory address?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
Can substring() be used in Java Streams?
Yes, you can use substring inside Java Streams while mapping elements. For instance, in a list of strings, you can apply .map(str -> str.substring(0, 3)) to extract substrings. This is useful for processing collections functionally and efficiently.
upgrad.com
upgrad.com › home › tutorials › software & tech › substring in java
Substring in Java – Syntax, Examples & Usage Explained
Can substring() return an empty string in Java?
Yes, if the beginIndex and endIndex are the same, substring returns an empty string. It doesn't throw an error because technically you're extracting zero characters. This is often used to validate input or split strings safely without causing exceptions.
upgrad.com
upgrad.com › home › tutorials › software & tech › substring in java
Substring in Java – Syntax, Examples & Usage Explained
How is substring() different from split() 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 a comma or space. They serve different purposes but can be used together for complex string parsing.
upgrad.com
upgrad.com › home › tutorials › software & tech › substring in java
Substring in Java – Syntax, Examples & Usage Explained
Videos
08:05
Java substrings are easy! 📧 - YouTube
substring And length (Java String Methods) - YouTube
03:31
substring method in java | substring in Java example | String ...
How does Java's substring method work? #java #computerscience #apcsa ...
05:42
Explain substring() method of String Class in Java (Core Java ...
05:13
Substrings in Java - YouTube
Codecademy
codecademy.com › docs › java › strings › .substring()
Java | Strings | .substring() | Codecademy
April 28, 2025 - In this example, substring(0, 11) extracts characters from index 0 (inclusive) to index 11 (exclusive). Yes, string indexing in Java starts at 0, so the first character of a string is at index 0.
DigitalOcean
digitalocean.com › community › tutorials › java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
February 20, 2025 - This method always returns a new string, and the original string remains unchanged because String is immutable in Java. In this tutorial, we’ll cover its syntax, use cases, and potential pitfalls while providing practical examples and solutions to common errors. public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)
Tutorialspoint
tutorialspoint.com › java › lang › string_substring_index.htm
Java.lang.String.substring() Method
package com.tutorialspoint; import ... String substr = ""; // prints the substring after index 7 till index 17 substr = str.substring(7, 17); System.out.println("substring = " ......
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › String.html
String (Java SE 17 & JDK 17)
April 21, 2026 - Replaces the first substring of this string that matches the given regular expression with the given replacement. ... Resolves this instance as a ConstantDesc, the result of which is the instance itself.
Upgrad
upgrad.com › home › tutorials › software & tech › substring in java
Substring in Java – Syntax, Examples & Usage Explained
May 14, 2025 - The return type of the substring() method is always a String. The method returns a new string containing the specified character range. It does not modify the original string (since strings are immutable in Java).
Tutorialspoint
tutorialspoint.com › java › lang › string_substring.htm
Java - String substring() Method
Following is an example to show the use of substring() method in Java by finding the substring of the provided string. Here both the beginning and the ending of the string is passed as parameters − · package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String str = "This is tutorials point"; String substr = ""; // prints the substring after index 7 till index 17 substr = str.substring(7, 17); System.out.println("substring = " + substr); // prints the substring after index 0 till index 7 substr = str.substring(0, 7); System.out.println("substring = " + substr); } } Let us compile and run the program above, the output will be displayed as follows − ·
Programiz
programiz.com › java-programming › library › string › substring
Java String substring()
If the endIndex is not passed, the substring begins with the character at the specified index and extends to the end of the string ... class Main { public static void main(String[] args) { String str1 = "program"; // 1st character to the last character · System.out.println(str1.substring(0)); // program // 4th character to the last character System.out.println(str1.substring(3)); // gram } }
Javatpoint
javatpoint.com › java-string-substring
Java String substring()
Java String substring() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string substring in java etc.
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-substring-method-example
Java String substring() Method with examples
Note: The method is called like this: substring(start + 1, end), here start index is +1 because, we want to get the substring after the delimiter position, however the end index is not +1 because end index is not inclusive in substring method. public class JavaExample{ public static void main(String[] args){ //given string String str = "Welcome to [BeginnersBook.com]"; //we would like to get the substring between '[' and ']' int start = str.indexOf("["); int end = str.indexOf("]"); String outStr = str.substring(start + 1, end); System.out.println(outStr); } }
Simplilearn
simplilearn.com › home › resources › software development › all you should know about substring in java
All You Should Know About Substring in Java | Simplilearn
September 11, 2025 - A substring is a part of another string. It's a commonly used method in java. Explore this tutorial to get an in-depth idea about substring in java. Start now!
Address 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
CodeGym
codegym.cc › java blog › strings in java › substring in java
Java String substring()
Here you will find the explanation of how using these methods, find out how they work and get some beneficial examples. Substring in general is a contiguous sequence of characters inside the String.
Published July 23, 2024
Top answer 1 of 6
4
It's supposed to be an efficiency measure. i.e. when you're taking a substring you won't create a new char array, but merely create a window onto the existing char array.
Is this worthwhile ? Maybe. The downside is that it causes some confusion (e.g. see this SO question), plus each String object needs to carry the offset info into the array, even if it's not used.
EDIT: This behaviour has now changed as of Java 7. See the linked answer for more info
2 of 6
4
Turning it around, why allocate a new char[] when it is not necessary? This is a valid implementation since String is immutable. It saves allocations and memory in the aggregate.