StringBuilder vs String

0 votes
I understand the difference between String and StringBuilder (StringBuilder being mutable) but is there a large performance difference between the two?

In what case is String builder a better choice?
Sep 21, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
568 views

1 answer to this question.

0 votes

To clarify what Gillian said about 4 string, if you have something like this:

string a,b,c,d;
 a = b + c + d;

then it would be faster using strings and the plus operator. This is because (like Java, as Eric points out), it internally uses StringBuilder automatically (Actually, it uses a primitive that StringBuilder also uses)

However, if what you are doing is closer to:

string a,b,c,d;
 a = a + b;
 a = a + c;
 a = a + d;

Then you need to explicitly use a StringBuilder. .Net doesn't automatically create a StringBuilder here, because it would be pointless. At the end of each line, "a" has to be an (immutable) string, so it would have to create and dispose a StringBuilder on each line. For speed, you'd need to use the same StringBuilder until you're done building:

string a,b,c,d;
StringBuilder e = new StringBuilder();
 e.Append(b);
 e.Append(c);
 e.Append(d);
 a = e.ToString();
answered Sep 21, 2018 by Upasana
• 8,620 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Intel Galileo Vs. Intel Edison?

Intel Galileo Key Features: Type: Single-Board Computer CPU: Intel Quark ...READ MORE

answered Aug 20, 2018 in IoT (Internet of Things) by DataKing99
• 8,240 points
1,896 views
0 votes
1 answer

What is the difference between IoTivity vs AllJoyn ?

Architecture: IoTivity provides 4 basic components: Discovery Data transmission Data Management Device ...READ MORE

answered Sep 6, 2018 in IoT (Internet of Things) by Annie97
951 views
0 votes
1 answer
0 votes
0 answers

Best way to reverse a string

I recently had to develop a string ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
356 views
0 votes
1 answer
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
1,415 views
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
697 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP