The char data type is only for a single character. But if you want to store multiple characters
together, like a name for instance, you can do that inside a string. In Java, there are multiple
ways of defining a string.
First way — using the String keyword
class HelloWorld { public static void main(String[] args) { String name = "Ernest";
System.out.println(name); }}Second way — using the new keyword
The new keyword is used to create a new object from a class.
class HelloWorld { public static void main(String[] args) { String name = new String("Ernest");
System.out.println(name); }}String methods
class Main { public static void main(String[] args) { String name = "Ernest"; String country = "ghana"; int age = 21;
System.out.println("Hello I am " + name + ", I am from " + country + " and I am " + age + " years old"); }}The above works well, but it can get complicated as more variables and plus operators pile up. There’s a better way — string formatting using format specifiers.
class Main { public static void main(String[] args) { String name = "Ernest"; String country = "ghana"; int age = 21;
String formattedString = String.format("My name is %s. I am from %s and I am %d years old.", name, country, age); }}This way, the code looks cleaner since we use format specifiers to tell the compiler which values we want to output, then add the values at the end in the order we want them displayed.
Format specifiers
%sor%S— string%f— decimal floating point%d— decimal integer%c— character%bor%B— boolean%tor%T— time and date%n— inserts a newline character
Instead of storing the String.format() result in a variable and printing the variable, you can
print it out directly. This works, but it’s not recommended — better to use the way above.
class Main { public static void main(String[] args) { System.out.println(String.format("formatted string here")); }}Checking the length of a string
class Main { public static void main(String[] args) { String name = "Ernest Sarfo";
System.out.println(name.length()); // also counts the spaces in the string }}Checking whether a string is empty
class Main { public static void main(String[] args) { String name = "Ernest Sarfo";
System.out.println(name.isEmpty()); // returns a boolean }}Converting between uppercase and lowercase
class Main { public static void main(String[] args) { String name = "Ernest Sarfo";
System.out.println(name.toUpperCase()); System.out.println(name.toLowerCase()); }}String comparisons
Note that when you use == to compare two strings, it just checks whether the object on the
right side is the same object as the one on the left side, which may not be true in most cases —
especially when the strings were created with the new keyword. To actually check whether the
content of two strings is equal, use the equals() method.
class Main { public static void main(String[] args) { String string1 = new String("abc"); String string2 = new String("abc");
System.out.println(string1.equals(string2)); // outputs true }}The above method is case sensitive — if string1 is "ABC" and string2 is "abc", it outputs
false. To compare the strings while ignoring case, use equalsIgnoreCase().
class Main { public static void main(String[] args) { String string1 = new String("abc"); String string2 = new String("ABC");
System.out.println(string1.equalsIgnoreCase(string2)); // outputs true }}Replacing part of a string
Use the .replace() method.
class Main { public static void main(String[] args) { String today = "The sky is blue";
System.out.println(today.replace("blue", "red")); }}Checking if a string contains a substring
Use the .contains() method.
class Main { public static void main(String[] args) { String today = "The sky is blue";
System.out.println(today.contains("sky")); // outputs true }}