![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 143
Rep Power: 4
![]() |
The immutability of strings
Quick question...
Just having read through stuff recently and trying to be a little more concious of efficiency in my programs I wasn't sure about this. I know that having the following String test = "James"; test += " is testing this"; Actually creates two strings, and it's better to use stringbuilder class to do this type of thing. However, does this String test = "James" + " is testing this"; Create one or two strings? I would assume one, but you know what they say about assuming! |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
Re: The immutability of strings
It creates 3. One ("James"), two (" is testing this"), and a third to store the result ("James is testing this")
Nursing the efficiency of String vs. StringBuilder is often not going to get you much benefit. Of course, there are loops that are vastly improved by the change, but often it's easier to just use String.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Re: The immutability of strings
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Re: The immutability of strings
I guess someone had to say that
![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 143
Rep Power: 4
![]() |
Re: The immutability of strings
Thanks for the info Jimbo, very useful, I had already played some with StringBuilder for the scrabble program just because having the number of string operations that I had (for 8!) was a HUGE overhead, changing to stringbuilder vastly improved performance.
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 143
Rep Power: 4
![]() |
Re: The immutability of strings
Efficiency isn't critical, but I like to try to program in an efficient manner, taking 10 seconds to do something that should take less than a second (if programmed in the same language) is bad practice.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Splitting strings into other strings | Keiro | C | 2 | Dec 11th, 2006 6:10 AM |
| inserting strings into other strings | codylee270 | C | 2 | Dec 3rd, 2005 9:37 PM |
| Question about multidimensional arrays of strings | aznluvsmc | C | 8 | Oct 15th, 2005 10:20 PM |
| creating Strings | pleasehelpmeeee | Java | 5 | Sep 26th, 2005 8:03 PM |
| pointers strings, and strcat() | conbrio | C | 3 | Apr 16th, 2005 1:23 PM |