Java String

Java Multiline String (with Examples)

Writing a multiline string can be approached differently depending on the version of Java you are using. Since Java 15, text blocks are the preferred method.

Java 21 String Templates

This Java tutorial discusses string templates in Java which is a new addition to the language in Java 21 as a preview feature.

Java String Tutorial

Java String class examples. Learn to create strings, methods, comparisons, formatting and various conversions with simple and easy to follow Java examples.

Java String Constant Pool

Learn about String class in Java, the motivation behind making it immutable, and the idea of a String constant pool. We will see how memory is manipulated when we create String instances via String literals or String constructors. Finally, we will go through the main advantages and disadvantages caused by …

Format a Phone Number with Regex in Java

Learn to format a specified string to a phone number pattern, which is ‘(123) 456-6789’. This conversion is generally required in applications where customer data has to be displayed, and phone number is part of this data. 1. Format String to ‘(###) ###-####‘ Pattern To format string to phone number …

Java String intern()

Learn to intern a string, and how the string literals differ from string objects. Java String.intern() is a native method and performs fast.

Java String contains()

Java String.contains() searches case-sensitive substring in a given string. It returns true if the substring is found, otherwise false.

Java String toLowerCase()

Java String toLowerCase() method transforms a String by converting all of its characters to lowercase, using the Locale rules if specified.

Java String toUpperCase()

Java String toUpperCase() transforms a string by replacing all lowercase characters to uppercase while leaving any characters already in uppercase.

Java String replaceAll()

The String.replaceAll(regex, replacement) in Java replaces all occurrences of a substring matching the specified regular expression with the replacement string. A similar method replace(substring, replacement) is used for matching the literal strings, while replaceAll() matches the regex. Note that in regular expressions, literal strings are also patterns, so replaceAll() will …

Java String replaceFirst()

Java String.replaceFirst() replaces the first occurrence of a substring found that matches the given argument substring (or regex).

Java String replace()

Java String replace() searches for a literal substring and replaces each occurrence with the replacement string beginning with index 0.

Java String concat()

Java String.concat() concatenates the argument string to the end of the current string and returns the combined string.

Java String substring()

Learn to find the substring of a string between the begin and end indices, and valid values for both indices with examples.

Java String lastIndexOf()

Java String.lastIndexOf() returns the last index of the specified character or substring in this string, if the substring is not found then it returns -1.

Java String indexOf()

Learn to find the location of a character or substring in a given string and at what index position using the String.indexOf() with examples.

Java String hashCode()

Java String hashCode() returns the hashcode for the String. The hash value is used in hashing-based collections like HashMap, HashTable etc.

Java String endsWith()

Java String.endsWith() verifies if the given string ends with a specified substring or not. It does not accept NULL and regex patterns.

Java String compareToIgnoreCase()

Java String compareToIgnoreCase() example compares two strings lexicographically ignoring case. Learn how it differs from equalsIgnoreCase().

Java String compareTo()

Java String compareTo() method example. Learn to compare two strings lexicographically. We can consider this string comparison dictionary based comparison.

Java String equalsIgnoreCase()

Java String.equalsIgnoreCase() compares the current string with the specified string in a case-insensitive manner. Learn with examples.

Java String.equals()

Learn to compare the content of two String objects in a case-sensitive manner using the String.equals() API. For case-insensitive comparison, we can use the equalsIgnoreCase() method. Never use ‘==’ operator for checking the strings equality. It verifies the object references, not the content, which is undesirable in most cases. 1. String.equals() API …

Java String charAt()

Java String.charAt() returns the character at specified index argument in the string object or literal. The argument must be a valid index.

Convert Float to String in Java

Learn to convert float value to String using Float.toString() and String.valueOf() methods and format float to n decimal points.

Convert Exception StackTrace to String in Java

Java program to convert error stack trace to String. StackTrace to String conversion may be useful when you want to print stack trace in custom logs in files or store logs in database.

Java StringJoiner

Java 8 StringJoiner joins strings with delimiter, prefix and suffix. Learn its usage with examples and differences with StringBuilder.

Convert long to String in Java

Java example to convert long to String in two different ways using String.valueOf(long l) and Long.toString(long l) methods. Both are static methods.

Convert String to long in Java

Java examples of converting a String to a long type using Long.parseLong(String), Long.valueOf(String) and new Long(String) constructor.

How to Unescape HTML in Java

Java examples unescapes an HTML string to a string containing the actual Unicode characters using StringEscapeUtils and a custom method.

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.