Java String Class
Overview
The String class provides various constructors and methods for creating and manipulating strings.
Compared with C, creating and manipulating strings is convenient because memory-related operators are not used directly.
String Constructors
| Constructor | Description |
|---|---|
| String() | Default constructor. Creates an object with no string. |
| String(String original) | Creates a string using another String object. |
| String(char value[]) | Creates an object using a character array. |
| String(char value[], int offset, int count) | Creates an object from part of a character array, starting at the first index and using the specified count. |
| String(int[] codePoints, int offset, int count) | (J2SE5) Creates an object using an array containing Unicode code points. |
| String(byte bytes[]) | Creates an object using a byte array. |
| String(byte bytes[], String charsetName) | Creates an object from a byte array using the specified charset name. |
| String(byte bytes[], Charset charset) | Creates an object from a byte array using the specified Charset. |
| String(StringBuffer buffer) | Creates an object using a StringBuffer object. |
| String(StringBuilder builder) | (J2SE5) Creates an object using a StringBuilder object. |
Creating Strings Without a Constructor
In Java, unlike other objects, a String object can be created without using a constructor.
The following example creates a string by using a literal without new.
package com.devkuma.tutorial.string;
public class StringJava {
public static void main(String[] args) {
String str = "This is java.";
System.out.println(str);
}
}
The result is as follows.
This is java.
Among Java classes, only the String class has this special behavior. Do not assume that other classes can behave the same way just because String does. There is no similar class, and you cannot create one.
Creating Strings: Why Not Use a Constructor?
As mentioned above, a String object can be created by assigning a literal without separately using new. It is also possible to use new and pass a literal as a parameter. However, strings created from literals are generally not created with new. Why is that? Reducing code is one reason, but there is also an internal reason.
The following example creates a string object by using new with a literal.
package com.devkuma.tutorial.string;
public class StringJava2 {
public static void main(String[] args) {
String str = new String("This is java.");
System.out.println(str);
}
}
As with the previous example, the result is the same.
This is java.
Even if the result is the same, two memory addresses may be created internally. One can be created for “This is Java.”, and another can be created for new String(“This is java”). If an object can be created in one operation, there is no need to do two operations and create two memory addresses. The garbage collector will handle memory, but because this is also related to slight performance overhead, it is better to avoid it when possible.
String Concatenation Without a Method
A String object can be concatenated by using the string operator “+” without using a method.
package com.devkuma.tutorial.string;
public class StringConcat2 {
public static void main(String[] args) {
String age = "30";
System.out.println("He is " + age + " years old");
}
}
It can also be concatenated with other data types that are not strings.
package com.devkuma.tutorial.string;
public class StringConcat3 {
public static void main(String[] args) {
int age = 30;
System.out.println("He is " + age + " years old");
}
}
Whenever an existing string changes, a new String object containing the changed string is created. The original string remains unchanged. It will be managed by the garbage collector.
Main String Methods
| Method | Description |
|---|---|
String Length
The length of a string means the number of characters included in the string. The length of a string can be found using the length() method.
Preparing...
Extracting Characters from a String
- char charAt(int index)
- void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
Comparing Strings
- boolean equals(Object anObject)
- boolean startsWith(String prefix, int toffset)
- boolean startsWith(String prefix)
- boolean endsWith(String suffix)
- int compareTo(String anotherString)
Searching Strings
These are used to search for substrings included in a string.
- int indexOf(int ch)
- int indexOf(int ch, int fromIndex)
- int lastIndexOf(int ch)
- int lastIndexOf(int ch, int fromIndex)
- int indexOf(String str)
- int indexOf(String str, int fromIndex)
- int lastIndexOf(String str)
- int lastIndexOf(String str, int fromIndex)
Converting Strings
- String substring(int beginIndex)
- String substring(int beginIndex, int endIndex)
- String concat(String str)
- String replace(char oldChar, char newChar)
- String replaceAll(String regex, String replacement)
- String replace(CharSequence target, CharSequence replacement)
- String trim()
- String toLowerCase()
- String toUpperCase()
Type Conversion
- String valueOf(Object obj)
- String valueOf(char data[])
- String valueOf(char data[], int offset, int count)
- String valueOf(boolean b)
- String valueOf(char c)
- String valueOf(int i)
- String valueOf(long l)
- String valueOf(float f)
- String valueOf(double d)