Learn to format string to phone number pattern which is (123) 456-6789. This conversion is generally required in applications where customer’s data has to be displayed and phone number is part of this data. How to format string to phone number with dashes To format string to phone number – Break the string to 3 […]
Learn how to get first 4 characters of a String or simply any number of first characters of a string in Java. Get first 4 characters of String – Example To get substring having first 4 chars first check the length of string. If string length is greater than 4 then substring(int beginIndex, int lastIndex) […]
Learn how to get last 4 characters of a String or simply any number of last characters of a string in Java.
The Java String intern() return the reference of a equal string literal present in string pool. String literals are automaically interned in Java.
The Java String contains() searches a substring in the given string. It returns true if substring are found in this string otherwise returns false. 1. String contains() method Use String.contains() to find is a substring is present in given string or not. Please remember that this method is case sensitive. 1.1. Method syntax The method […]
Java String toLowerCase() returns a string which is result of converting all characters in given string to lowercase. Convert string to lowercase in Java.
The Java String toUpperCase() returns a string which is result of converting all characters in given string to UPPERCASE. Read more: Java example capitalize the string 1. String toUpperCase() method Use String.toUpperCase() converts any string to UPPECASE letters. 1.1. Method syntax String toUpperCase() method is overloaded and comes in two variants. 1.2. ‘null’ is not […]
The Java String split() returns string array after it splits the string around matches of the method argument regular expression. 1. String split() method Use String.split() to split a string to string array by tokenizing with delimiter substring or regular expression. 1.1. Method syntax String split() method is overloaded and comes in two variants. 1.2. […]
The Java String replaceAll() returns a string after it replaces each substring of that matches the given regular expression with the given replacement. 1. String replaceAll() method Use String.replaceAll(String regex, String replacement) to replace all occurrences of a substring (matching argument regex) with replacement string. 1.1. Method syntax 1.2. throws PatternSyntaxException Watch out that replaceAll() […]
The Java String replaceFirst() method replaces the first substring ‘regex’ found that matches the given argument substring (or regular expression) with the given replacement substring. The substring matching process start from beginning of the string (index 0). 1. String replaceFirst(String regex, String replacement) method The String replaceFirst() method uses regular expression to find and replace […]
The Java String replace() method replaces each substring of this string that matches the literal target substring. The matching process starts at index 0.
The Java String concat() method concatenates the method argument string to the end of string object. 1. String concat(String str) method Internally, Java creates a new character array with combined length of string object and argument string, and copies all content from both strings into this new array. Finally, combiner character array is converted to […]
Java String substring() example. Learn to get substring of a string from given start index to end index. A valid index is between 0 and length of string.
Java String lastIndexOf() method. It returns the last index of specified character or string as method argument. If argument is not found it returns -1.
The Java String indexOf() method returns the index of given character or string as method argument. If argument is not found in string, method returns -1. The index counter for a string starts from zero.
Java String hashCode() method returns the hash code for the String. Hash code value is used in hashing based collections like HashMap, HashTable etc. This method must be overridden in every class which overrides equals() method. Read More: Contract between hashCode() and equals() methods 1. String hashCode() method The hash code for a String object […]
Java String endsWith() method is used to check the suffix of the string. It verifies if given string ends with argument string or not. 1. Java String endsWith(String str) method Java program to check if a string ends with suffix argument string ‘str’. Program output. String endsWith() method does not accept regular expression as argument. […]
Java String startsWith() method check if a string starts with prefix argument string. does not accept regex as argument. null argument is also not allowed.
Java String compareToIgnoreCase() example compares two strings lexicographically ignoring case. Difference in compareToIgnoreCase() vs equalsIgnoreCase().
Java String compareTo() method example. Learn to compare two strings lexicographically. We can consider this string comparison dictionary based comparison.