Series: Strings

this series contains string, stingbuffer and stringbuilder related entries.

compareTo() String Example

java.lang.String class comes with many methods to check two strings contain same characters in the same sequence. To do this, String class comes with equals(), compareTo(), equalsIgnoreCase() and compareToIgnoreCase() methods. This tutorial is on compareTo() String. Now we discuss compareTo() String method. Following is the method signature as defined in String class. public int compareTo(String …

compareTo() String Example Read More »

compareToIgnoreCase() String Example

java.lang.String class comes with many methods to check two strings contain same characters in the same sequence. To do this, String class comes with equals(), compareTo(), equalsIgnoreCase() and compareToIgnoreCase() methods. This is tutorial is on compareToIgnoreCase() String. We have seen earlier compareTo() method which does case-sensitive comparison lexicographically. Now we discuss compareToIgnoreCase() method which does …

compareToIgnoreCase() String Example Read More »

contentEquals() String Example

We know earlier equals() method is used to compare two strings whether they have same sequence of characters or not. But this method cannot compare String with StringBuffer. Even if they have same characters, the equals() returns false. In this case use contentEquals() String. contentEquals() String method is useful to compare String with StringBuffer or …

contentEquals() String Example Read More »

Java String reverse with and without reverse() Example

To reverse a string with less code in Java, there comes reverse() method in StringBuffer class. reverse() method cannot be used directly on string as reverse() is not defined in String class but defined in StringBuffer and StringBuilder. String should be converted to StringBuffer and then applied reverse() method. 1. Reversing using reverse() method. public …

Java String reverse with and without reverse() Example Read More »

toCharArray() String Example

java.lang.String class comes with toCharArray() String method to convert string to a character array. The signature is given of toCharArray() String as defined in Java API. public char[] toCharArray(): Returns a character array comprising the string characters as elements. In the following toCharArray() String is used to get a char array out of string. public …

toCharArray() String Example Read More »

String Constructors Java

In Java, a String object can be created a number of ways. The most frequently used styles are shown here using 9 types of String constructors (including StringBuffer and StringBuilder) defined java.lang.String API. The 9 constructors are 1. public java.lang.String(); 2. public java.lang.String(java.lang.String); 3. public java.lang.String(char[]); 4. public java.lang.String(char[], int startIndex, int numChars); 5. public …

String Constructors Java Read More »

String Capitalize Java

Java comes with toUpperCase() and toLowerCase() methods that converts all the characters of the string into uppercase or lowercase. String Capitalize example keeps all String characters in capital letters. Following are the method signatures. String toLowerCase(): Converts all the characters of the string into lowercase. Returns a string with all lowercase letters. Remember, the original …

String Capitalize Java Read More »

Precaution length vs length() vs size() Java

length vs length() vs size(): All the three length, length() and size() give the number of elements present but in different contexts. A) length variable: In Java, array (not java.util.Array) is a predefined class in the language itself. To find the elements of an array, designers used length variable (length is a field member in …

Precaution length vs length() vs size() Java Read More »

Convert StringBuffer to String

After knowing the differences between String, StringBuffer and StringBuilder, let us Convert StringBuffer to String and vice versa. It is very much required in coding. Convert StringBuffer to String A StringBuffer object can be converted to String using toString() method of Object class. StringBuffer sb1 = new StringBuffer(“hello”); StringBuffer sb2 = new StringBuffer(“hello”); String str1 …

Convert StringBuffer to String Read More »

String contains() Java

Existence of a substring in the main string can be checked with String contains() method. public class Demo { public static void main(String args[]) { String str1 = “abcdefghi”; // string (main string) String str2 = “def”; // another string (substring) String str3 = “ded”; // another string (substring) boolean b = str1.contains(str2); // checks …

String contains() Java Read More »

String convert byte array char array

String convert byte array char array Summary: Many a times it is required to convert the string characters into a char array or byte array; of course, very often, the reverse is also required. Discussed at length in this tutorial "String convert byte array char array". Program converting strings to char array and byte array …

String convert byte array char array Read More »

String vs StringBuffer vs StringBuilder

Three classes exist in java.lang package to manipulate strings – String, StringBuffer and StringBuilder. Even though all the three are used for manipulating strings only but they differ in many aspects and specifically used in the code where demanded. Following tables give the differences between these and when to use. Now let us tabulate the …

String vs StringBuffer vs StringBuilder Read More »