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.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.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