Java-new String vs String literals

new String vs String literals When you create String object using new operator, it always create a new object in heap(one of memory section). On the other hand, if you create object using string literal, it may return an existing object from String pool(one of memory section), if it’s already exists. Otherwise it will create a … Read more…

Java-Get Substring

Get Substring A substring is a part of another string. For example, the strings “123” and “45” are substrings of the string “12345”. The subString( ) method of the String class enables us to extract the substrings from a string. A substring( ) method of the form substring( int startIndex, int endIndex-1 ) will return a … Read more…

Get Specific Index

Java-Get Specific Index

Get Specific Index The String class provides two methods, indexOf( ) and lastIndexOf( ), that allows you to get index of a specified character or starting index of the specified substring. The indexof() method has four forms: The lastIndexOf( ) method has four forms: These all methods return the index at which the character or substring … Read more…

Java-Trimming

Trimming To remove whitespace from the beginning and end of a string (but not the interior) you can use the trim( ) method. Program Program Source public class Javaapp { public static void main(String[] args){ String str1 = new String(” haj sof “); System.out.println(str1); System.out.println(“str1.length = “+str1.length()); str1 = str1.trim(); System.out.println(str1); System.out.println(“str1.length = “+str1.length()); } … Read more…

Java-Startswith and Endswith

Startswith and Endswith The String class provides two more methods that are specialized versions of the equals( ) method. The startWith( ) method enables us to check whether a given string str1 starts with another string str2. For example, in the following program, the startWith( ) method will return true as the string “hajsoftutorial” starts … Read more…

RegionMatches

Java-RegionMatches

RegionMatches The regionMatches () method of String compares a specific region inside a string with another specific region in another string. There is an overloaded form that allows you to ignore case in such comparisons. It has two forms: For both versions, ‘toffset’ specifies the index at which the region begins within the invoking String object. … Read more…