StringBuilder Construction
This class has four overloaded constructors. Three are explained here.
This constructor creates an empty StringBuilder object, as shown in the following statement:
Characters can then be appended to the StringBuilder object, st.
public StringBuilder(CharSequence seq)
This constructor takes a string literal as an argument, as shown by following code segment:
The output is:
public StringBuilder(String str)
This constructor method, takes a string identifier as an argument, as shown in the following code segment:
StringBuilder st1 = new StringBuilder(st);
System.out.println(st1);
The output is:
StringBuilder Methods
public StringBuilder append(char c)
This appends a character to the StringBuilder object, which might be empty. The following code segment illustrates this:
st.append('T'); st.append('h'); st.append('i'); st.append('s'); st.append(' ');
st.append('i'); st.append('s');
System.out.println(st);
The output is:
public StringBuilder append(CharSequence s)
This appends a string sequence (string literal). The following code segment illustrates this:
The output is:
public StringBuilder append(String str)
This uses a string identifier for appending. The following code segment illustrates this:
The output is:
public int length()
This method returns the number of characters in the string, as shown in the following code segment:
int sz = st.length();
System.out.println(sz);
The output is:
The length is 19, meaning there are 19 characters.
public char charAt(int index)
This method returns a copy of the char at the index. Index counting begins from 0. The for-loop in the following code segment, returns all the characters of the StringBuilder object:
The output is:
public void setCharAt(int index, char ch)
The character at an index position can be changed. Index counting begins from 0. The following code segment uses this method to change, ‘i’ in “This” to ‘u’, making “Thus”:
sb.setCharAt(2, 'u');
System.out.println(sb);
The output is:
public void setLength(int newLength)
This method changes the length of the string. If the given length is smaller than that of the original string, there is truncation. The counting of newLength begins from 1. The following code segment truncates the string to a length of 7:
sb.setLength(7);
System.out.println(sb);
The output is:
public int indexOf(String subStr)
Index counting begins from 0. This returns the start index of the first occurrence of the substring given as argument. The following code is an illustration:
The output is 2, for the “is” in “This”.
public int indexOf(String str, int fromIndex)
Index counting begins from 0. This returns the first occurrence of the substring given as first argument, beginning at the value of the second argument. The following code illustrates this:
The output is 12.
public StringBuilder deleteCharAt(int index)
This deletes a particular character. In the following code segment, characters at index 8, index 9, index 10, and index 11 are deleted. Both the old and new strings are changed simultaneously each time.
The output is:
This is dancer.
public StringBuilder delete(int start, int end)
In the previous code, when the character at index 8 was deleted, the next one became the character at index 8. Instead of deleting one by one, the characters from index 8 to index 11 can be deleted in one command, as the following code shows:
The output is:
This is dancer.
Note that the method’s end index (12) is not included in the effective range.
public StringBuilder replace(int start, int end, String str)
A sub-string in the original string, can be replaced by another string, str, which may be longer or shorter than the original string. The following code segment illustrates this:
The output is:
Here: the dancer.
Again, the end index is one higher than the actual end of the range.
public StringBuilder insert(int offset, char c)
This inserts a character into the string, increasing the length by one. Offset is the index where the character will be inserted. The counting of offset begins from 1 and not 0. On the right of what is inserted, the characters in the string are shifted one place to the right. In the following code, ‘s’ is inserted between ‘r’ and ‘.’ :
The output is:
This is the dancers.
The method returns a new string. Both the old and new strings are modified. The output sentence is not good English, and it will be addressed below:
public StringBuilder insert(int offset, char[] str)
Remember that offset counting in Java, begins from one, not zero. With that, a sub-string can be inserted into the original string, as in the following code:
The output is:
This is the big dancer.
Both the original and the return strings had the change.
public StringBuilder insert(int index, char[] str, int offset, int len)
A sub-string from another string can be inserted into the original string, as illustrated in the following code sample:
The output is:
This is the big dancer.
The substring portion taken from the other string is: len – offset, e.g. 8 – 4 to get “big”.
Conclusion
The string literal cannot be edited with the String class in Java. The StringBuilder class is a string class whose literal can be edited. The class is in the java.lang.* package and does not need importation. This class has constructors and methods. The more commonly used ones have been explained in the previous sections. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.
from https://ift.tt/YimZ7Bn
0 Comments