Substring. Strings are immutable. We cannot delete characters from them to get a substring. Instead we must copy a range of characters (from a begin index to an end).
With substring, we pass one or two indexes. We must specify a begin index, which is where the substring copying starts. The end index, optional, is where the substring ends.
An example. Here the first substring call takes the first three letters (from index 0 to index 3). The character at index 3, "s," is not included.
Then We assign a new String reference (cat) to the result of substring. This is a separate String.
Finally We take a second substring. We use indexes that are based on the String length. We reduce it by an offset.
public class Program {
public static void main(String[] args) {
String value = "cats and dogs";
// Take substring of first three letters.
String cat = value.substring(0, 3);
System.out.println(cat);
// Take substring of three letters.// ... Indexes based on value length.
String dog = value.substring(value.length() - 4,
value.length() - 1);
System.out.println(dog);
}
}cat
dog
Begin index. When we use just one argument, this is the begin index. The end index is automatically equal to the last index. This allows for simpler syntax.
public class Program {
public static void main(String[] args) {
String value = "website";
// Take substring starting at index 3.
String sub = value.substring(3);
System.out.println(sub);
value = "newspaper";
// Take substring starting at index 4.
sub = value.substring(4);
System.out.println(sub);
}
}site
paper
SubSequence. This method does the same thing as substring, but returns instead a reference to a CharSequence. Unless you need a CharSequence, this method is probably not needed.
public class Program {
public static void main(String[] args) {
String value = "programmer";
// Get sequence excluding first and last three chars.
CharSequence data = value.subSequence(3, value.length() - 3);
System.out.println(data);
}
}gram
SubSequence, use. The Java documentation tells us why subSequence exists on the String class. It is required for String to implement the CharSequence interface. It has no other purpose.
This method is defined so that the String class
can implement the CharSequence interface.
One char, charAt. Often we need to get a single character from a String. Substring can do this, but charAt is a clearer option. It returns a char, not a String object, so is likely faster.
Note It may be better to use substring for a one-char string if other parts of a program require a String.
public class Program {
public static void main(String[] args) {
String letters = "abc";
// Get one char from the String.
char letter2 = letters.charAt(1);
System.out.println(letter2);
// Get one char String object.
String letter2String = letters.substring(1, 2);
System.out.println(letter2String);
}
}b
b
StringBuilder, append substring. We often need to append substrings to a StringBuilder. But the substring call is not needed here. We can append a CharSequence directly from the string.
So Usually we can avoid substring() before calling append() on the StringBuilder.
import java.lang.StringBuilder;
public class Program {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder();
String value = "abcdef";
// Append first 3 chars from String to StringBuilder.// ... No substring is needed.// ... String is a CharSequence.
builder.append(value, 0, 3);
System.out.println(builder);
}
}abc
A review. A substring is another String. In Java we acquire substrings with a begin index and an end index. In calling this method, we create a new string.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 10, 2023 (simplify).