[JAVA] StringBuilder Note

StringBuilder, which is a mutable string. It seems to be used to concatenate strings at high speed. Again, we will use the StringBuilder methods to learn how to use them.

append You can add a character string using append. You can add various types such as boolean type, char type, String type, and array as a character string.

StringBuilder sb = new StringBuilder("Good morning");

//boolean
boolean bool = true;
System.out.println(sb.append(bool));

//char
char c = 'A';
System.out.println(sb.append(c));


StringBuilder sbr = new StringBuilder("Mr. A");

//Array
String[] str = {"Hello","Good night"};
System.out.println(sbr.append(str[0]));
System.out.println(sbr.append(str[1]));

StringBuilder s = new StringBuilder("Mr. A");

//int,double,float,long
int i = 100;
double d = 100;
float f = 100;
long l = 100;
System.out.println(s.append(i));
System.out.println(s.append(d));
System.out.println(s.append(f));
System.out.println(s.append(l));
//boolean
Good morning true
//char
Good morning trueA
//Array
A's Hello
A Mr. Good night Hello
//int
Mr. A 100
//double
Mr. A 100100.0
//float
Mr. A 100100.0100.0
//long
Mr. A 100100.0100.0100

charAt(int index) Returns the char value for the specified index in this sequence.

StringBuilder sb = new StringBuilder("ABCDs");

System.out.println(sb.charAt(0));
System.out.println(sb.charAt(2));
System.out.println(sb.charAt(4));
A
C
s

delete(int start, int end) Deletes the characters in the substring of this sequence. start --Start index (including this value). end --End index (not including this value).

StringBuilder sb = new StringBuilder("ABCDs");

sb.delete(1, 3);
System.out.println(sb);
ADs

deleteCharAt(int index) Deletes the char at the specified position in this sequence.

StringBuilder sb = new StringBuilder("ABCDs");

sb.deleteCharAt(2);
System.out.println(sb.toString());
ABDs

indexOf(String str) Returns the index of the position where the specified substring first appears in this string.

StringBuilder sb = new StringBuilder("ABCDs");

System.out.println(sb.indexOf("C"));
System.out.println(sb.indexOf("s"));
2
4

indexOf(String str, int fromIndex) Returns the index of the position where the specified substring first appears in this string after the specified index.

StringBuilder sb = new StringBuilder("ABCDsD");

System.out.println(sb.indexOf("D",2));
System.out.println(sb.indexOf("s",0));
3
4

lastIndexOf(String str) Returns the index of the position where the specified substring appears on the far right within this string.

StringBuilder sb = new StringBuilder("ABCDssD");

System.out.println(sb.lastIndexOf("D"));
System.out.println(sb.lastIndexOf("s"));
6
5

length() Returns the length (number of characters).

StringBuilder sb = new StringBuilder("ABCDssD");

System.out.println(sb.length());
7

replace(int start, int end, String str) Replaces the characters in the substring of this sequence with the characters in the specified String.

StringBuilder sb = new StringBuilder("ABCDssD");

System.out.println(sb.replace(2, 5, "r"));
ABrsD

reverse() Replace this character sequence with the reverse sequence order.

StringBuilder sb = new StringBuilder("ABCDssD");

System.out.println(sb.reverse());
DssDCBA

setCharAt(int index, char ch) The character of the specified index is set to ch.

StringBuilder sb = new StringBuilder("ABCDssD");

sb.setCharAt(1, 's');
System.out.println(sb.toString());
AsCDssD

setLength(int newLength) Sets the length of the character sequence.

StringBuilder sb = new StringBuilder("ABCDssD");

System.out.println(sb.length());
sb.setLength(20);
System.out.println(sb.length());
7
20

subSequence(int start, int end) Returns a new character sequence that is a subsequence of this sequence. start --Start index (including this value). end --End index (not including this value).

StringBuilder sb = new StringBuilder("ABCDssD");

System.out.println(sb.subSequence(0, 2));
System.out.println(sb.subSequence(4, 5));
AB
s

substring(int start), substring(int start, int end) Returns a new String containing a partial sequence of characters currently contained in this character sequence.

StringBuilder sb = new StringBuilder("ABCDssD");

System.out.println(sb.substring(2));
System.out.println(sb.substring(5));

System.out.println(sb.substring(5,6));
System.out.println(sb.substring(0,2));
CDssD
sD

s
AB

insert Insert the specified value in the specified index

StringBuilder sb = new StringBuilder("ABCDssD");

//0th'P'Insert
sb.insert(0, 'P');
System.out.println(sb.toString());
//Insert 23 in 8th
sb.insert(8, 23);
System.out.println(sb.toString());
int[] i = {10,20,30,40};
//Insert index 2 of i array at 0th
sb.insert(0, i[2]);
System.out.println(sb.toString());
PABCDssD
PABCDssD23
30PABCDssD23

capacity() Returns the current capacity.

StringBuilder s = new StringBuilder();
//Initial capacity is 16 characters
System.out.println(s.capacity());
//Initial capacity 16+Additional 14 characters with current capacity of 30 characters
StringBuilder sb = new StringBuilder("ABCDssDAAAAAAA");
System.out.println(sb.capacity());
16
30

trimToSize() Attempts to reduce the storage used in this character sequence.

StringBuilder sb = new StringBuilder("A");
//Initial capacity 16+Current capacity is 17 characters with 1 additional character
System.out.println(sb.capacity());
//16 characters for the initial capacity are deleted, 1 character
sb.trimToSize();
System.out.println(sb.capacity());

StringBuilder s = new StringBuilder("AAAAAAAAAA");
//Initial capacity 16+Additional 10 characters with current capacity of 26 characters
System.out.println(s.capacity());
//16 characters for the initial capacity are deleted, 10 characters
s.trimToSize();
System.out.println(s.capacity());
17
1

26
10

Recommended Posts

StringBuilder Note
Note
Note
[Note] Dockerfile
Rails Credentials Note
[Note] About nil
[Note] SQLite related
Firebase Authentication Note