The String type is defined in a class named String found in a file named String.java.
The String type is a reference type meaning that a variable of type String holds a reference (memory address on the Heap) to where an instance of the String class is actually stored.
The String class provides
- storage for a sequences of characters that define the String
- a ton of method that allow us to retrieve information about the String and manipulate the String
We can create strings using a String literal or a constructor, or a combination of variables and literals.
String str0 = "cat"; String str1 = new String(); String str2 = new String("hat"); char[] data = {'c', 'a', 't'}; String str3 = new String(data); // "cat" String str4 = new String(data, 1, 2); // "at" byte[] ascii = {99, 97, 116}; String str5 = new String(ascii); // "cat" String str6 = new String(ascii, 1, 2); // "at" String str7 = str0 + " in the " + str2; // concatenation String str8 = new String(str7); String str9 = str8;
Equality
String s1 = "hello"; String s2 = s1; String s3 = "hello"; String s4 = new String(s1); System.out.println(s1==s2?"equal":"unequal"); System.out.println(s1==s3?"equal":"unequal"); System.out.println(s1==s4?"equal":"unequal");
We can call class methods on an instance of a class using the dot operator as shown below.
The equals() method compares characters and the equalsIgnoreCase(), well, compares characters while ignoring the case of the characters. Both methods return a boolean value.
String s5 = "Hello"; System.out.println(s1==s5?"equal":"unequal"); System.out.println(s1.equals(s5)?"equal":"unequal"); System.out.println(s1.equalsIgnoreCase(s5)?"equal":"unequal");
Operations on Strings
Obtaining the Length of a String
You can find the length of a string by using the length() method.
String player = "Bob"; int len = player.length(); // 3
String Concatenation
int age = 20; String mssg0 = "Age: " + age;
If one of the operand to the + operator is a String, the compiler converts the other operand to a String. So be careful.
String mssg1 = "Age: " + age + 1; // "Age: 201" String mssg2 = "Age: " + (age + 2); // "Age: 21"
Creating New Strings From Old Strings
Strings are immutable, so you can’t modify them – but you can construct new string using various String class methods.
String str1 = "scattered".substring(6); String str2 = "scattered".substring(1,4); // doesn't include endIndex String str3 = "scattered".replace('c', 'h'); // "shattered" String str4 = "scattered".replace("sc", "ch"); // "chattered" //trim() removes leading and trailing whitespace from a string. String str5 = " some info ".trim(); // "some info" //join() concatenates two or more strings separated by a delimiter. String str = String.join(":", "one", "two", "tee"); // "one:two:tee"
Character Extraction
// Get a single character at a particular index char ch = "hello".charAt(4); // 'o' // Get more than one character String str = "hello world"; char[] buffer = new char[10]; str.getChars(6, 2, buffer, 3); // copies "wo" to buffer[3] // Get all of the characters char[] buffer = str.toCharArray();
Searching Strings
String str1 = "one two tee"; String str2 = "two"; System.out.println(str1.regionMatches(0, str2, 0, 3)?"true":"false"); System.out.println(str1.regionMatches(4, str2, 0, 3)?"true":"false"); System.out.println("peanut".startsWith("pea")?"true":"false"); System.out.println("peanut".endsWith("nut")?"true":"false"); System.out.println("hello".indexOf('e')); // prints 1 System.out.println("hello".indexOf("lo")); // prints 3 System.out.println("cat in the hat".indexOf('t', 4)); // prints 7 System.out.println("cat in the hat".indexOf("at", 4)); // prints 12
© 2017, Eric. All rights reserved.