Java String Concatenation: The Best Way? (+Examples)
This article delves into the differences between these ways and highlights how Java optimizes the bytecode to make the String concatenation faster.
This article delves into the differences between these ways and highlights how Java optimizes the bytecode to make the String concatenation faster.
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.
Using the TextTable class or String.format() methods, we can easily align the text in columns and print them in the console or any output file.
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 class examples. Learn to create strings, methods, comparisons, formatting and various conversions with simple and easy to follow Java examples.
Learn to remove the last character from a String, either using the indices or only matching criteria. Learn to handle null and empty values.
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 …
Learn to write a java program to remove all the white spaces from a given string using regular expression (“\\s”) and isWhitespace() method.
Learn to indent (left indentation) a string(s) in Java using String.indent() API. This api has been introduced in Java 12.
Learn to use String.isBlank() method to determine is a given string is blank or empty or contains only white spaces. isBlank() method has been added in Java 11.
Learn to use String class’s strip(), stripLeading() and stripTrailing() methods to remove unwanted white spaces from a given string in Java 11.
Learn to repeat a given string N times, to produce a new string which contains all the repetitions, though a simple Java program using String.repeat() api.
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 …
Learn how to get first 4 characters of a String or simply any number of first characters of a string in Java.
Learn how to get last 4 characters of a String or simply any number of last characters of a string in Java.
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() searches case-sensitive substring in a given string. It returns true if the substring is found, otherwise false.
Java String toLowerCase() method transforms a String by converting all of its characters to lowercase, using the Locale rules if specified.
Java String toUpperCase() transforms a string by replacing all lowercase characters to uppercase while leaving any characters already in uppercase.
Java String split() returns an array after splitting the string using the delimiter or multiple delimiters such as common or whitespace.
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() replaces the first occurrence of a substring found that matches the given argument substring (or regex).
Java String replace() searches for a literal substring and replaces each occurrence with the replacement string beginning with index 0.
Java String.concat() concatenates the argument string to the end of the current string and returns the combined string.
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() returns the last index of the specified character or substring in this string, if the substring is not found then it returns -1.
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() returns the hashcode for the String. The hash value is used in hashing-based collections like HashMap, HashTable etc.
Java String.endsWith() verifies if the given string ends with a specified substring or not. It does not accept NULL and regex patterns.
Learn to use the String.startsWith() and StringUtils class for checking a String prefix against a single value or multiple values.
Java String compareToIgnoreCase() example compares two strings lexicographically ignoring case. Learn how it differs from equalsIgnoreCase().
Java String compareTo() method example. Learn to compare two strings lexicographically. We can consider this string comparison dictionary based comparison.
Java String.equalsIgnoreCase() compares the current string with the specified string in a case-insensitive manner. Learn with examples.
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() returns the character at specified index argument in the string object or literal. The argument must be a valid index.
This article presents a simple Java program to find duplicate characters in a String. You can modify the code to find non-repeated characters in string.
Learn to convert float value to String using Float.toString() and String.valueOf() methods and format float to n decimal points.
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 examples to right pad a string with spaces or zeros to make it fixed length using the StringUtils, Strings classes and custom solution.
Java examples to left pad a string with spaces, zeros or any other pad character using the StringUtils, Strings classes and custom solution.
Java 8 StringJoiner joins strings with delimiter, prefix and suffix. Learn its usage with examples and differences with StringBuilder.
Learn to remove extra white spaces between words in a String in Java. Do not forget to check if string has leading or trailing spaces. Trim such Strings.
Learn how to trim leading and/or trailing whitespaces from a Java String using regular expressions and a new String.strip() API in Java 11.
Learn to reverse all the characters of a Java string using the recursion and StringBuilder.reverse() methods.
Learn to reverse each word in a sentence in Java with example. We will reverse words in string using StringBuilder and StringUtils classes.
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.
Java examples of converting a String to a long type using Long.parseLong(String), Long.valueOf(String) and new Long(String) constructor.
In Java, converting an int value to String is not difficult, but knowing which method may save us a few CPU cycles is worth knowing.
Learn to convert a String to title case using the WordUtils class and create a custom solution by splitting and capitalizing each word.
Java examples unescapes an HTML string to a string containing the actual Unicode characters using StringEscapeUtils and a custom method.
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.