A String is a sequence of characters. In Java strings are Objects of type String.
String is not one of the primitive data types (https://fastfoodcoding.com/tutorials/1504109138872/java-primitive-data-types) in java.
You can initialize a String with following code.
String name = "foo"; // most common way of initializing a String
You can also initialize String with below code.
String name = new String("foo");
Most used String operations
String class in Java has tons of methods to perform different operations with it. Following are the most commonly used operations.
String LengthYou can find the length of using the length() method. Following example shows you that
String name = "foo"; int length = name.length(); System.out.println("Length of the name String is: " + length); // Output: Length of the name String is: 3
String ConcatenationYou can join one or more Strings using the operator.
String one = "Hello"; String two = "World"; System.out.println(one + " " + two); // output: Hello World
String Concatenation with other data typesYou can also concatenate Strings with other data types, in those cases the combination will be another String. The other data types will automatically get converted into their respective String representation.
int number = 7; String message = "is a prime number"; System.out.println(number + " " + message); // output: 7 is a prime number
indexOf & charAt MethodsYou can find the index of a particular character in the String by using indexOf method. indexOf returns the first occurrence of the specified character
You can use charAt method to find out the character at a given index.
String message = "abcdefghabcdefghabcdefgh"; int cIndex = message.indexOf('c'); System.out.println("index of character c is: " + cIndex); // output: index of character c is: 2
String message = "abcdefgh"; char secondLetter = message.charAt(2); System.out.println("character at 3rd postion is: " + secondLetter); // output: character at 3rd postion is: c
toLowerCase() & toUpperCase()
As the name says you can change the case of the String using toLowerCase()/ toUpperCase()
String message = "abcdefgh"; String uppperCaseMessage = message.toUpperCase(); System.out.println("This should come in uppercase" + uppperCaseMessage); String lowerCaseMessage = uppperCaseMessage.toLowerCase(); System.out.println("This should come in lowercase" + lowerCaseMessage); /* output: This should come in uppercaseABCDEFGH This should come in lowercaseabcdefgh */
replace & replaceAll
You can replace a single character or a series or characters using replace method. If you want to replace a particular pattern (a regular expression) then you should use the replaceAll
String message = "abcd1234abcd"; String replaceUsage = message.replace("123", "-"); System.out.println(replaceUsage); // output: abcd-4abcd String replaceAllUsage = message.replaceAll("\\d+", "-"); System.out.println(replaceAllUsage); // output: abcd-abcd
Note: It's a misconception that replace only replaces the first occurrence where as replaceAll replaces all the occurrences, thats not true.
Both will replace all the occurrences where the replace look for a character sequence and replaceAll look for a pattern.
Comparing two Strings equals() vs ==
== compares the references of Strings, there can be a chance that the content of Strings are same but the references to those Strings are different. So you should stick to equals() method only if you want to compare two Strings.
String one = new String("foo"); String two = new String("foo"); System.out.println(one == two); // output: false System.out.println(one.equals(two)); // output: true