

Say you have been given a list of names and want to extract the names with the surnames ‘Kumar’. substring() method comes in very handy when you want to extract the prefix or suffix of a string.total number of characters present in the string If your startIndex or endIndex is greater than the length of the string i.e.This is against the signature rules of the substring() method. startIndex is greater than the endIndex.You need to be careful while specifying the parameters of the substring() method as it may throw StringIndexOutOfBoundsException if you make any of the following mistakes – Substring() method helps us in getting the desired subset of the string. Type 1:- If we specify both startIndex and endIndex We can also just specify the startIndex and it will return the characters from that index till the end.

You can specify mainly two parameters here- startIndex and endIndex, where startIndex is inclusive and endIndex is exclusive, which means startIndex will include the index specified and endIndex will not include the index specified. This character sequence is maintained as a array called value, for example. Substring() method is a part of the Java String class and returns part of the string. String in java is a sequence of characters. Its immutability doesn’t let the value be changed.
#SUBSTRING JAVA EXAMPLE PASSWORD#
Say you have passed your username and password in the database as String, you’d want that nobody can change your data and that’s exactly what String helps us in. String immutability helps in making our application more secure. Because of this, we save a lot of heap space because different String variables can refer to the same String variable present in the pool. It is possible only because of string’s immutability. In Java runtime, we have a String pool maintained. The reason why it is immutable is that, suppose you have five reference variables pointing to one object- “Anjali”, if the value of the object is changed by one reference variable then all the reference variables will be affected, that is why the immutability of strings is important. In Java, Strings are immutable, that means, String once created is unmodifiable, you cannot change it. The length of the string can be found using the length() method. Here, we have created a string named name which contains “Vineeta Tiwari” as a value.
#SUBSTRING JAVA EXAMPLE CODE#
String name = "Vineeta Tiwari" Code language: Java ( java )
