Modifying A String

There are about 5000 built-in packages of java. And each of these packages contains numerous classes. An important built-in package of java is java.lang. This package is automatically imported into every program that you write. Within this package, there is an important class known as String. Explaining the full string class will cost your whole day, thus we limit our discussion only to few things about string which includes, how to declare a string and how we can further use it in our java code.

If you are familiar with few programming languages (other than java) then surely you are not new to the thing that some of these languages implement strings as character arrays. But this is not the case with java. Java implements strings as objects of type String (the class in java.lang package). This feature of java allows a programmer to work with strings easily i.e, it makes string handling a bit user-friendly. Also you can construct a string object in many ways. You can also get a Java programming certification to validate your Java skills.

A unique thing about string objects in java is that once created, you cannot change them. By the way of explanation, you cannot change the characters that compromise a string. But don’t worry, whenever java makes some restrictions, it also provides an alternate solution to deal with it.

According to java, you are only not allowed to change the string object, but you can still modify that object. There are several ways by which you can create a string object and hence declare a string. But for now, we take the simplest way to declare the string, which is as follows; String S= “Hello”;. In the previous statement, a string object is created (which is on right-hand side), and is represented by the string literal. While S plays the role of a reference variable.

How to Modify a String?


From the whole discussion that we’ve made above, we are very clear that strings are immutable i.e, once created they cannot be changed. Thus, to modify them we use the following methods;
1) substring(): Using this method, you can extract a part of originally declared string/string object. This method can be used in two ways:-
a) String substring(int startIndex): Using the startIndex, you specify the index from where your modified string should start. Thus, your new string will from the specified index number and runs to the end. For example, consider a string declared as, String S=”conversion”; thus, String S1=S.substring(2); this gives us S1 as “nversion”.
b) String substring(int startIndex, int endIndex): In this method, you give the starting point as well as the ending point of the new string. For example, String S2= S.substring(2,4); gives us S2=”nve”;

You can also learn about casting in Java.

2) concat(): Using this function you can concatenate two strings. By this we mean, that you can combine the two strings using concat(). You can even use this function to further add characters to the original string. For instance, if you have a string, String S1=”Hello”; and you need to add more characters to S1 then you can write, and String S2=S1.concat(” Everyone”);. This gives you the value of S2 as “Hello everyone”.

3) replace(): This method is used to modify the original string by replacing some characters from it. You can replace the characters in two ways:
a) String replace(char original, char replacement): This method replaces one and only character from the original string. For example, String S=”Hello”.replace(‘e’, ‘x’); gives us Hwllo as the value of S.
b) String replace(CharSequence original, CharSequence replacement): Unlike the above method, which replaces only one character at a time, using this method you can replace a sequence of characters in one go. An example for this method can be given as String S=”He is a good boy”.replace(“good”, “bad”); gives us an output; He is a bad boy.

4) trim(): Sometimes it may happen that you leave some extra spaces in the beginning  or in the end of your original string. When you print these kind of strings, they give an odd look. But since you cannot change the value of these strings you can simply modify them by removing these extra white spacesFor example, if you have declared a string by writing, String S1=”Hey there brown cow”; which contains a lot of white spaces in the beginning as well as in the end. Thus, you can improve it by using the trim function on it. You can write, String S2=S1.trim(); which removes the extra white spaces.

Thus, to conclude I would like to say that if java had given a rule which says that once declared we cannot change the value of a string then on the other hand java has also provided us the above four methods to modify the declared original string. If you’re new to the Java development, start your journey by learning the Java version history.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.
Scroll to Top