5 Examples of substring() inward Java

SubString inwards Java is a useful method from java.lang.String class, which is used to create smaller String from bigger ones. The way Substring industrial plant prior to Java 1.7 tin displace create a subtle retentivity leak because both String and their substring shares same grapheme array. Which means, if you lot guide maintain a large String of 200MB too created a substring of 2MB from that, that could prevent 200MB String from beingness garbage collected. I concur this doesn't human face normal too indeed was a bug, but it was similar that till Java 1.6 too it's diverse update. One reason, which I could think, why Java designer initially idea similar that, peradventure to relieve retentivity past times sharing char array too to make, creating substring faster past times simply copying pointers, instead of data. Nevertheless, this was reported every bit põrnikas too Oracle have fixed it, hence no to a greater extent than substring retentivity leak issue inwards Java 7.


This number doesn't undermine the importance of substring method, which is 1 of the most useful methods from java.lang.String class. One thing, which is too worth remembering is that, whenever you lot telephone shout out upward substring method, it furnish a separate String object, because String is immutable inwards Java. In side past times side section, nosotros volition encounter the syntax of substring method too How to work it for practical purpose.




SubString inwards Java - Description, Syntax too Example

 SubString inwards Java is a useful method from  v Examples of substring() inwards JavaSubstring method is overloaded inwards Java, too nosotros guide maintain 2 variants of it, outset convey 1 parameter, simply get down index too minute convey 2 parameters, get down index too goal index every bit shown below:

public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)


In the instance of the outset method, substring starts alongside beginIndex too goes till the goal of String, while, inwards the instance of an overloaded  method, substring starts from beginIndex too goes till endIndex -1. Since String inwards Java is goose egg indexes based,  beginIndex can endure from 0 to length of String. Let's encounter couplet of examples of substring method to acquire how they work:

public class SubStringTest {      public static void main(String[] args) {      String quote = "Java is to JavaScript what Car is to Carpet";                                                                            // Example 1 - Let's say, nosotros postulate entirely "what Car is to Carpet" substring         // out start volition endure 22, you lot tin displace too practice quote.indexOf("what")       // too nosotros tin displace outset substring method                                                                                                      String substr = quote.substring(22);                                 System.out.println(substr);                                                                                                               // Example 2 - Let's say, nosotros postulate "Java" from quote                              // start volition endure 0 too goal volition endure iv (because goal is exclusive)                                                                           String substr2 = quote.substring(0,4);                               System.out.println(substr2);                                                                                                              // Example three - Following telephone shout out upward to substring method volition furnish empty String     String substr3 = quote.substring(quote.length());                    System.out.println(substr3.isEmpty());                                                                                                    // Example iv - substring method volition too furnish empty String if get down = goal     String substr4 = quote.substring(3,3);                               System.out.println(substr4.isEmpty());                                                                                                    // Example v - This substring telephone shout out upward volition throw IndexOutOfBoundException           String substr5 = quote.substring(-1);  // start < 0                  String substr6 = quote.substring(2,1); // start > goal                      }  }
Output  what Car is to Carpet Java true true Exception inwards thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1  at java.lang.String.substring(Unknown Source)  at java.lang.String.substring(Unknown Source)
 
Remember substring(int beginIndex) method volition throw IndexOutOfBoundException if, an index is less than goose egg or to a greater extent than than length of String. While substring(int beginIndex, int endIndex) volition throw IndexOutOfBoundException if beginIndex is negative, or larger than endIndex or end > length().

It's too worth knowing that substring volition furnish empty String if you lot transcend the length of String every bit a start inwards the outset version too same index every bit start too goal inwards the minute method, every bit shown inwards our substring instance inwards Java.


Important points close substring() inwards Java

  1. There are 2 substring() method, outset convey entirely starting indicate spell minute human face both starting too goal points.
    public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)
  2. The string starts alongside index 0 i.e. outset grapheme volition endure at index 0. So if you lot desire to take away the outset character, simply practice substring(1), it volition furnish substring without outset character, equal to deleting the outset character.
  3. In the minute method, startIndex is inclusive but endIndex is exclusive, which agency if you lot desire to take away the final grapheme hence you lot postulate to hand substring(0, string.length()-1).
  4. The substring() method volition furnish an empty String if beginIndex= endIndex.
  5. The substring() method volition throw IndexOutOfBoundsException if start < 0 or start > end.
too hither is a prissy summary slide of all the of import things you lot learned close the substring() method inwards this article past times doing those examples:

 SubString inwards Java is a useful method from  v Examples of substring() inwards Java




That's all close substring inwards Java. nosotros guide maintain seen How to work substring to create smaller String from a large text. We guide maintain too compared 2 overloaded version of substring past times dissimilar substring examples, too too touched based on memory leak issue due to substring, which is instantly fixed inwards JDK 1.7.


Further Reading
Java Programming Interview Exposed
How to contrary String without StringBuffer inwards Java
How to work String matches method inwards Java alongside Regular Expression
Java String trim() Example to take away white spaces
Java String indexOf() example
How to split upward String past times comma inwards Java
Difference betwixt String too StringBuffer inwards Java
Core Java, Volume 1 ninth Edition past times Cay S. Horstmann


Subscribe to receive free email updates:

0 Response to "5 Examples of substring() inward Java"

Posting Komentar