String Utils Methods Flashcards

1
Q

Declaring String Buffer

A

StringBuffer stringbuffer = new StringBuffer();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Convert a String to StringBuffer

A

StringBuffer stringbuffer = new StringBuffer(str);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Convert a StringBuffer to String

A

String str = stringbuffer.toString();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Character at a particular place of String Buffer

A

stringbuffer.charAt(nth);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Adding String

A

stringbuffer.append(string1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Reverse a String

A

stringbuffer.reverse();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Insert a String

A

stringbuffer.insert(Start Index,string);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Delete a part of the string

A

stringbuffer.delete(Start Index,End Index);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Delete char at the nth place

A

stringbuffer.deleteCharAt(nth);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Replace a part of the String

A

stringbuffer.replace(Start Index,End Index,string);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Substring

A

stringbuffer. substring(Start Index);

stringbuffer. substring(Start Index,End Index);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Length of the string

A

stringbuffer.length()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Difference between String and StringBuffer

A

1) Mutability: String is immutable (Once created, cannot be modified) while StringBuffer is mutable (can be modified).
2) Performance: While performing concatenations you should prefer StringBuffer over String because it is faster. The reason is: When you concatenate strings using String, you are actually creating new object every time since String is immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Difference between StringBuffer and StringBuilder

A

Lets summarize the differences in detail:

1) Synchronization: StringBuffer methods are synchronized while StringBuilder methods are non-synchronized, it means that for thread-safe operations you must choose StringBuffer class instead of StringBuilder.
2) Performance: In a synchronized environment a single thread can perform a certain operation rather than distributing the work among multiple threads, which makes StringBuffer low performer as it is synchronized. StringBuilder performance is better than StringBuffer because it is not synchronized.
3) Which one to use: Operations (without considering the performance) are almost same in both the classes which means there is nothing in StringBuffer which cannot be done using StringBuilder. As discussed above the main thing which you need to consider while making a choice is thread-safety, if you think that the operation should be thread-safe then use StringBuffer, in all other cases StringBuilder is a better choice as it offers you the same functionality with better performance.

Similarities: Unlike String, both StringBuffer and StringBuilder are mutable (can be modified).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly