Java is a powerful and versatile programming language used by millions of developers all over the world. With its extensive library of packages and classes, there’s almost no limit to what can be achieved with the language. One of the useful Java commands for manipulating strings of text is the Java-Substring command. In this article, we’ll look at what the command does, how to use it, and why it’s relevant to developers.
What is Java-Substring?
Java-Substring, or java.lang.String.substring(), is a string manipulation command found in the java.lang package. A substring is a portion of text taken from a longer string of characters and stored in a new string object. The newly created substring includes all the characters found in the original string, but between two points denoted by indices. The purpose of extracting a substring is to manipulate it in some way, such as replacing or removing characters within the substrung.
The substring command is a powerful tool for manipulating strings in Java. It can be used to extract a specific portion of a string, or to remove a portion of a string. It can also be used to replace a portion of a string with a new string. The substring command is an essential part of any Java program that requires string manipulation.
Creating a Substring in Java
Creating a substring in Java is relatively straightforward. The command requires two parameters; a start index and an end index. The start index is the point from which the substring should begin, and the end index marks the end point. For example, to create a small substring from a larger string “This is a Sample Text,” the Java command might look like this:
String substring = “This is a Sample Text”.substring(4,9);
This command begins at character 4 and ends at character 9, resulting in a substring with the contents “ is a.” Substrings don’t include their start or end indices, so “is a” is all that will be included.
It is important to note that the start index is inclusive, while the end index is exclusive. This means that the substring will include the character at the start index, but not the character at the end index. For example, if the start index is 0 and the end index is 3, the substring will include the first three characters of the string.
Accessing Characters in a Substring
Now that we have created a substring object, we can access each existing character within the object. To do this, we need to use the Java charAt command. The syntax of the command looks like this:
char c = substring.charAt(index);
Here we use the variable ‘c’ to represent a single character from within the substring. The variable ‘index’ serves as the reference point for whichever character we wish to access. For example, if our original substring contains “is a,” we can use the following command to isolate the spaces between “i” and “s”:
char c = substring.charAt(1);
The output of this command will be ‘c = ‘ ‘; in other words, c is assigned the value of ‘ ‘ (blank space).
We can also use the charAt command to access characters from the end of the substring. To do this, we use a negative index value. For example, if our substring contains “is a,” we can use the following command to isolate the “a”:
char c = substring.charAt(-1);
The output of this command will be ‘c = ‘a’; in other words, c is assigned the value of ‘a’.
Iterating Through a Substring
In addition to accessing characters singly, it’s also possible to iterate through all characters within a given substring. This requires the use of a looping statement such as a for loop, together with the Java length() method and charAt() command. The following code shows an example of how to iterate through a substring:
for(int i=0; i<substring.length(); i++) { char c = substring.charAt(i); System.out.println(c);}
In this case, we execute as many instances of the charAt() command as there are characters in the substring, printing each character as we go.
Comparing Two Substrings
In some instances, you may wish to compare two separate substrings by determining whether they have identical characters at each point. There are two commands in Java that can be used for this kind of comparison: equals() and equalsIgnoreCase(). The former command checks for exact matches between characters, whereas the latter does not consider case.
For example, if we want to know if the two substrings “superman” and “Superman” are equal, we would need to use equalsIgnoreCase():
boolean comparison = substring1.equalsIgnoreCase(substring2); System.out.println(comparison); // Outputs 'true'
Replacing Characters in a Substring
An extremely useful feature of Java substring commands is their ability to replace characters directly within the substring itself. While strings are immutable in Java and cannot be modified directly, they can be stored in other objects to be altered accordingly. This is achieved using the replace() command.
For example, suppose we have a substring containing “Hello World” and wish to replace all instances of ‘o’ with ‘$’; this is done with the following command:
String newSubstring = substring.replace("o","$");System.out.println(newSubstring); // Outputs 'Hell$ W$rld'
As you can see, all occurrences of ‘o’ have been replaced by ‘$’ in the new substring object.
Finding the Length of a Substring
Sometimes it’s useful to know how long a given substring is; in other words, how many characters are present within it. To find the length of a given substring, Java provides us with the length() method. This command simply requires the name of the object in which the substring is stored:
int length = substring.length();
The output should equal the amount of characters within the original string; for example, if our substring consists of “Hello” its length would be 5.
Trimming a Substring
It’s also possible to trim sections from either end of your substring using methods known as trimStart() and trimEnd(). These commands are used to remove undesired whitespace characters such as tabs or carriage returns. The syntax for each command looks like this:
String trimmed= stringName.trimStart() //for trimming left side String trimmed= stringName.trimEnd() //for trimming right side
To demonstrate how these methods work, let’s take an example string with an undesired tab character on either end:
" Hello World\t"
Using trimStart() and trimEnd() respectively, we can remove the tab character from either side:
"Hello World"
We can check that this has been successful by asking for the substring’s length:
"Hello World".length() //outputs 11.
Splitting a Substring
Finally, java substring commands are also handy for splitting substrings into distinct parts and then storing each part in separate string objects; this is known as delimiting. To do this, Java provides us with the split() method:
String[] split= substringName.split("delimiter");
This method requires you to specify a delimiter – that is, a character which will separate each part of your substring when split. For instance, if our substring is “apples-oranges-bananas” and our delimiter is ‘-‘, then our output should consist of three separate strings: “apples”, “oranges” and “bananas”.
Conclusion
- A Java substring is a portion of text taken from a longer string of characters, denoted by two indices.
- You can access individual characters within substrings using charAt(), iterate through them using looping statements, compare them using equals() or equalsIgnoreCase(), replace them using replace(), find their length using length(), and trim them using trimStart() and trimEnd().
- Split() allows you to delimit your substrings into individual parts.
Java-Substring is an incredibly useful tool for developers working with large amounts of text-based data; it simplifies manipulation of strings and allows us to achieve complex outcomes with minimal effort. Knowing how to harness its power gives developers far greater control over their programs.