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.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.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.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.copyValueOf() The method copyValueOf() converts a character array to a String with the same contents. This method is equivalent to valueOf(char[]). The offset represents the index of the first element to start copying from, and the count represents the number of elements to copy. Available Signatures [source,java,gutter:,false] public static… Continue Reading copy-value-of

Java String.concat() The method concat() concatenates two Strings. Simply put, it connects them into a single String. If the length of the argument is 0, then the method simply returns the String object. Available Signatures [source,java,gutter:,false] public String concat(String str) Example [source,java,gutter:,true] @Test public void whenCallConcat_thenCorrect() { assertEquals(“elephant”, “elep”.concat(“hant”)); }

Java String.getBytes() The method getBytes() encodes a String into a byte array using the platform’s default charset if no argument is passed. We can pass a specific Charset to be used in the encoding process, either as a String object or a String object. Available Signatures [source,java,gutter:,false] public byte[] getBytes()… Continue Reading get-bytes

Java String.isEmpty() The method isEmpty() is a convenience method that checks if the size of a String is equal to zero. Available Signatures [source,java,gutter:,false] public boolean isEmpty() Example [source,java,gutter:,true] @Test public void whenCallIsEmpty_thenCorrect() { String s1 = “”; assertTrue(s1.isEmpty()); }

Java String.replace() 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 replace

Java String.subSequence() The method subSequence() obtains a part of a String given the starting index and the length of the result. The method SubSequence() behaves in the same way as substring(). The only difference is that it returns a CharSequence instead of a String. Available Signatures [source,java,gutter:,false] public CharSequence subSequence(int… Continue Reading sub-sequence