Additional - Strings

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

String chart

Java maintains a pool of string literals to help save memory. When a new string literal is created, Java checks the Pool for a matching string. If found, the new variable references the pooled string. If not, the new string is added to the Pool.

Method Return Runtime
charAt(int index)charO(1)
codePointAt(int index)intO(1)
equals(Object anObject)booleanO(n)
equalsIgnoreCase(String str)booleanO(n)
length()intO(1)
split(String regex)String[]O(n)
substring(int beginIndex)StringO(n)
substring(int beginIndex, int endIndex)StringO(n)
toCharArray()char[]O(n)
toLowerCase()StringO(n)
toUpperCase()StringO(n)
trim()StringO(n)
indexOf(String str)intO(n*m)

Examples:

String str = "Hello world";
String str2 = "Hello World";
int len = str.length();
String lower = str.toLowerCase();
String upper = str.toUpperCase();
String trimmed = str.trim();
String subString = str.substring(0, 3);
boolean isContains = str.contains("world");
char ch = str.charAt(0);
int code = str.codePointAt(0);
boolean equal = str.equals("hello");
boolean equalsIgnoreCase = str.equalsIgnoreCase(str2);
int compare = str.compareTo(str2);
String[] splitted = str.split(" ");

// static methods
String joined = String.join("-", splitted);
System.out.println(joined);

StringBuilder:

Java StringBuilder class is used to create mutable (modifiable) String.

Constructors:

MethodSummary
StringBuilder()Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(String str)Constructs a string builder initialized to the contents of the specified string.

Methods:

MethodReturn Type
append(String str)StringBuilder
reverse()StringBuilder
toString()String
substring(int start)String
substring(int start, int end)String
length()int
indexOf(String str)int
charAt(int index)char
delete(int start, int end)StringBuilder
insert(int offset, String str)StringBuilder

Example:

// Initialize a new StringBuilder with some text
StringBuilder sb = new StringBuilder("Hello, StringBuilder!");

// 1. Append text to the end
sb.append(" It's a powerful tool.");
System.out.println("After append: " + sb);

// 2. Insert text at a specified index
sb.insert(7, "Java ");
System.out.println("After insert: " + sb);

// 3. Replace part of the text between start and end index
sb.replace(7, 11, "Awesome");
System.out.println("After replace: " + sb);

// 4. Delete part of the text between start and end index
sb.delete(7, 15);
System.out.println("After delete: " + sb);

// 5. Delete a single character at a specific index
sb.deleteCharAt(5);
System.out.println("After deleteCharAt: " + sb);

// 6. Reverse the entire text
sb.reverse();
System.out.println("After reverse: " + sb);
sb.reverse();  // Reverse again to get back to original order

// 7. Set a character at a specific index
sb.setCharAt(0, 'h');
System.out.println("After setCharAt: " + sb);

// 8. Get the current length and capacity of StringBuilder
System.out.println("Length: " + sb.length());
System.out.println("Capacity: " + sb.capacity());

// 9. Convert StringBuilder to String
String result = sb.toString();
System.out.println("StringBuilder to String: " + result);