+ operator rules:
IMPORTANT STRING METHODS
int length()char charAt(int index)int indexOf(int ch)int indexOf(int ch, int fromIndex)int indexOf(String str)int indexOf(String str, int fromIndex)indexOf() returns –1 when no match is found.String substring(int beginIndex)String substring(int beginIndex, int endIndex)String toLowerCase()String toUpperCase()boolean equals(Object obj)boolean equalsIgnoreCase(String str)equals() takes an Object rather than a String. This is because the method is the same for all objects. If you pass in something that isn’t a String, it will just return false.boolean startsWith(String prefix)boolean endsWith(String suffix)String replace(char oldChar, char newChar)String replace(CharSequence target, CharSequence replacement)boolean contains(CharSequence charSeq)String strip()String stripLeading()String stripTrailing()String trim()String intern()String string = "animals"; System.out.println(string.charAt(0)); // a System.out.println(string.charAt(6)); // s System.out.println(string.charAt(7)); // throws exception
String string = "animals";
System.out.println(string.indexOf('a')); // 0
System.out.println(string.indexOf("al")); // 4
System.out.println(string.indexOf('a', 4)); // 4
System.out.println(string.indexOf("al", 5)); // -1String string = "animals";
System.out.println(string.substring(3)); // mals
System.out.println(string.substring(string.indexOf('m'))); // mals
System.out.println(string.substring(3, 4)); // m
System.out.println(string.substring(3, 7)); // mals
System.out.println(string.substring(3, 3)); // empty string
System.out.println(string.substring(3, 2)); // throws exception
System.out.println(string.substring(3, 8)); // throws exceptionSystem.out.println("abc".strip()); // abc
System.out.println("\t a b c\n".strip()); // a b c
String text = " abc\t ";
System.out.println(text.trim().length()); // 3
System.out.println(text.strip().length()); // 3
System.out.println(text.stripLeading().length()); // 4
System.out.println(text.stripTrailing().length());// 4First, remember that \t is a single character. The backslash escapes the t to represent a tab.
> [!Note:]
You don’t need to know about Unicode for the exam. But if you want to test the difference, one of Unicode whitespace characters is as follows:
Unicode whitespace characters is as follows:
char ch = '\u2000';
10: String alpha = ""; 11: for(char current = 'a'; current <= 'z'; current++) 12: alpha += current; 13: System.out.println(alpha); 15: StringBuilder alpha = new StringBuilder(); 16: for(char current = 'a'; current <= 'z'; current++) 17: alpha.append(current); 18: System.out.println(alpha);
abcdefghijklmnopqrstuvwxyz
4: StringBuilder sb = new StringBuilder("start");
5: sb.append("+middle"); // sb = "start+middle"
6: StringBuilder same = sb.append("+end"); // "start+middle+end"4: StringBuilder a = new StringBuilder("abc");
5: StringBuilder b = a.append("de");
6: b = b.append("f").append("g");
7: System.out.println("a=" + a);
8: System.out.println("b=" + b);printed:
a=abcdefg b=abcdefg
There are three ways to construct a StringBuilder:
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder("animal");
StringBuilder sb3 = new StringBuilder(10);IMPORTANT STRINGBUILDER METHODS
charAt(), work exactly the same as in the String class.indexOf(), work exactly the same as in the String class.length(), work exactly the same as in the String class.substring(), work exactly the same as in the String class.StringBuilder append(String str)StringBuilder insert(int offset, String str)StringBuilder delete(int startIndex, int endIndex)StringBuilder deleteCharAt(int index)StringBuilder replace(int startIndex, int endIndex, String newString)StringBuilder reverse()String toString()3: StringBuilder sb = new StringBuilder("animals");
4: sb.insert(7, "-"); // sb = animals-
5: sb.insert(0, "-"); // sb = -animals-
6: sb.insert(4, "-"); // sb = -ani-mals-
7: System.out.println(sb);
jshell> StringBuilder sb = new StringBuilder("animals");
jshell> sb.insert(8, "-");
| Exception java.lang.StringIndexOutOfBoundsException: offset 8, length 7
| at String.checkOffset (String.java:4576)
| at AbstractStringBuilder.insert (AbstractStringBuilder.java:1170)
| at StringBuilder.insert (StringBuilder.java:336)
| at (#11:1)StringBuilder sb = new StringBuilder("abcdef");
sb.delete(1, 3); // sb = adef
sb.deleteCharAt(5); // throws an exception
StringBuilder sb = new StringBuilder("abcdef");
sb.delete(1, 100); // sb = aStringBuilder builder = new StringBuilder("pigeon dirty");
builder.replace(3, 6, "sty");
System.out.println(builder); // pigsty dirtyStringBuilder builder = new StringBuilder("pigeon dirty");
builder.replace(3, 100, "");
System.out.println(builder);prints: pig
StringBuilder did not implement equals().
You can call toString() on StringBuilder to get a String to check for equality instead.
THE STRING POOL
The string pool, also known as the intern pool, is a location in the Java virtual machine (JVM) that collects all these strings.
String x = "Hello World"; String y = "Hello World"; System.out.println(x == y); // true
String x = "Hello World"; String z = " Hello World".trim(); System.out.println(x == z); // false
Since it isn’t the same at compile-time, a new String object is created.
String singleString = "hello world"; String concat = "hello "; concat += "world"; System.out.println(singleString == concat);
This prints false. Concatenation is just like calling a method and results in a new String.
String x = "Hello World";
String y = new String("Hello World"); //create a new String
System.out.println(x == y); // falseString name = "Hello World";
String name2 = new String("Hello World").intern();
System.out.println(name == name2); // true15: String first = "rat" + 1;
16: String second = "r" + "a" + "t" + "1";
17: String third = "r" + "a" + "t" + new String("1");
18: System.out.println(first == second); // true
19: System.out.println(first == second.intern()); // true
20: System.out.println(first == third); // false
21: System.out.println(first == third.intern()); // true