Convert String to String Array in Java

In this tutorial, we will learn how to Convert String to String Array in Java. While working on Strings, we may need to convert a big String into an array of Strings. The String class in Java provides a method split() to split the String into an array of Strings. The split() method splits the string as per the regular expression passed to it. The split() method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are…

Read More

Convert String to InputStream in Java

In this tutorial, we will learn how to Convert String to InputStream in Java. While working on Strings, sometimes we come across big String that we want to process it incrementally or a small portion of it at a time. Then in this scenario, converting String to InputStream will be useful. InputStream is an abstract class representing an input stream of bytes. It implements the Closeable interface. This abstract class is the super class of all classes. Most of the methods in this class will throw an IOException on error…

Read More

StringTokenizer countTokens() Method

In this tutorial, we will learn about StringTokenizer countTokens() method with an example. StringTokenizer in Java is a class which allows a String to break into tokens. This class is a legacy class that is retained for compatibility reasons. StringTokenizer implements the Enumeration interface. Therefore, given an input string, you can enumerate the individual tokens contained in it using StringTokenizer. StringTokenizer class provides countTokens() method to calculate the number of times that this tokenizer’s nexToken method can be called before it generates an exception. If we want to calculate the no. of tokens in a String…

Read More

Java String trim() method

In this tutorial, we will learn about Java String trim() method. String class in Java provides trim() method to remove the leading and trailing whitespaces in a String. The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. The syntax of trim() method is: Syntax of trim() method:

The trim( ) method is quite useful when you process user commands. Example:

Output:

If you observe the output, the leading and trailing whitespaces have been removed after…

Read More

String equals() vs == in Java

In this tutorial, we will learn about String equals() vs == in Java. Two Strings in Java can be compared using equals() and == operator in Java. equals() and == are used to compare Strings, there is a difference between the operations between these two. The equals( ) method compares the characters inside a String object. == operator compares two object references to see whether they refer to the same instance. The equals() method compares the value inside String instances irrespective if the two object references refer to the same…

Read More

Java String Split() method

In this tutorial, we will learn about Java String split() method. The String class represents character strings. All string literals in Java programs, such as “abcde”, are implemented as instances of this class. String class in Java provides split() method. Java String split() method splits the String in sub strings based on delimiter or regular expression. String class has two variants of split() method. The syntax of these methods are:

this method splits the string based on the regex provided to it and returns the array of strings.

this…

Read More

Java String contains() Method

In this tutorial we will see how to use Java String contains() method. String class in Java provides contains() method to check whether a particular string is part of another string or not. Java String contains() method returns true/false. The syntax of contains() method is:

contains() method checks whether a particular string is part of another string or not. Parameters: str – the sequence to search for Returns: true if this string contains str, false otherwise Throws: NullPointerException – if str is null Example:

Let’s run this program and…

Read More

Convert String to Date in Java

Sometimes we need to Convert String to Date in Java. In this tutorial we will learn about how to convert String to Date in Java. SimpleDateFormat is a concrete class for formatting and parsing dates. SimpleDateFormat class in Java provides a method parse() to parse the date in String format and format() method to convert Date in the required date format. Syntax of parse() method:

This method parses text from the beginning of the given string to produce a date. Parameters: str – String which needs to be parsed/to…

Read More

Convert boolean to String in Java

In some scenarios, we may need to Convert boolean to String in Java. A boolean can be converted to String in java in following ways. 1) Using String.valueOf(boolean b): The String.valueOf(boolean b) method takes boolean value as an input and returns the String representation of the boolean argument. Syntax:

Parameters: b – a boolean. Returns: a string representation of the boolean argument. 2) Using Boolean.toString(boolean b): This method is same as String.valueOf(boolean b) method and returns a String object representing this Boolean’s value. Syntax:

Parameters: b – a Boolean to…

Read More

Convert double to String in Java

In some scenarios, we may need to Convert double to String in Java. A double can be converted to String in java in following ways. 1) Using String.valueOf(double d): The String.valueOf(double d) method takes double value as an input and returns the String representation of the double argument. Syntax:

Parameters: d – a double. Returns: a string representation of the double argument. 2) Using Double.toString(double d): This method is same as String.valueOf(double d) method and returns a String object representing this Double’s value. Syntax:

Parameters: d – a Double to…

Read More