Java String.String() String objects can be created by either using literals: String s = “a string”; or by calling one of the constructors: String s = new String(“a string”); If we use the String literal, it’ll try to reuse already existing object from the String constant pool. On the other hand,… Continue Reading constructor

Java String.format() The method format() formats a String using a format String and arguments. For example, characters ‘s’ and ‘S’ evaluate to “null” if the argument arg is null. If arg implements Formattable, then the method Formattable, then the method arg.formatTo() is invoked. Otherwise, the result is evaluated by invoking… Continue Reading format

Java String.lastIndexOf() The method lastIndexOf() returns the index of the last occurrence of a String in another String. If an int is passed to the method, then the method searches for the Unicode character equivalent. We can also pass the index of the character to start searching from. Available Signatures… Continue Reading last-index-of

Java String.endsWith() The method endsWith() is a convenience method that checks if a String ends with another given String. If the argument is an empty String, then the method returns true. Available Signatures [source,java,gutter:,false] public boolean endsWith(String suffix) Example [source,java,gutter:,true] @Test public void whenCallEndsWith_thenCorrect() { String s1 = “test”; assertTrue(s1.endsWith(“t”));… Continue Reading ends-with

Java String.intern() The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool. Note that, if another String with the same contents exists in the String constant pool, then a new object won’t be created and the new… Continue Reading intern

Java String.indexOf() The method indexOf() returns the first occurrence index of a character or a String in another String. We can pass the index of the character to start searching from. Note that the method returns -1 if the passed value is not found. Available Signatures [source,java,gutter:,false] public int indexOf(int… Continue Reading index-of

Java String.contains() The method contains() checks if a String contains another String. The method accepts a CharSequence. So, we can pass any of the implementing classes to it such as StringBuilder and StringBuffer. Available Signatures [source,java,gutter:,false] public boolean contains(CharSequence s) Example [source,java,gutter:,true] @Test public void whenCallContains_thenCorrect() { String s =… Continue Reading contains

Java String.valueOf() The method valueOf() has several overloads that accept one parameter of different types and convert them to a String. Examples include boolean, char, char array, double, int and long. We can also convert a part of a char array to a String by passing: offset – the index… Continue Reading value-of

Java String.regionMatches() The method regionMatches() checks if two String regions are equal. Here are a few important points: ignoreCase specifies whether we should ignore the case of both Strings toffset determines the starting index of the first String other specifies the second String. ooffset specifies the starting index of the… Continue Reading region-matches