How to split a String in Java

In this post we will discuss on how to split a String using Java classes such as String, StringTokenizer and Scanner. String.split(delimiter) String.split(delimiter) splits the text at the given delimiter and creates an array of String objects. Output But when you want to split the text using pipe symbol (|), you need to escape that … Read more

How to convert byte array to String

In this post, we will discuss various methods to convert byte array to String. String(byte[]) This String constructor variant constructs a new String by decoding the bytes array using the default charset. String(byte[], Charset) This String constructor variant constructs a new String by decoding the bytes array using given Charset. Since, this constructor throws UnsupportedEncodingException, … Read more

How to Sort an ArrayList of Strings

To sort an ArrayList of Strings, we will use Collections class. Collections class in java.util consists of static methods written using polymorphic algorithms that process given collection and return a new collection. Below we discuss two variants of the sort method. Both sort methods require collection of objects that implement the Comparable interface providing the … Read more

How to Convert List to Array in Java

Any List can be converted into array of objects by using its toArray method. A string representation of that array can be generated using Arrays.deepToString method. Output:

How to round to 2 decimal places in Java

NumberFormat class from java.text package can be used to format numbers. This class provides setMaximumFractionDigits method which can be used to round to 2 decimal places. Output: