Click here to Skip to main content
15,897,704 members
Home / Discussions / C#
   

C#

 
GeneralRe: Why does this not change my string? Pin
PIEBALDconsult15-Aug-11 16:01
mvePIEBALDconsult15-Aug-11 16:01 
AnswerRe: Why does this not change my string? Pin
Luc Pattyn15-Aug-11 16:30
sitebuilderLuc Pattyn15-Aug-11 16:30 
AnswerRe: Why does this not change my string? Pin
fcronin15-Aug-11 12:08
fcronin15-Aug-11 12:08 
GeneralRe: Why does this not change my string? Pin
stephen.darling15-Aug-11 12:14
stephen.darling15-Aug-11 12:14 
GeneralRe: Why does this not change my string? Pin
fcronin16-Aug-11 2:39
fcronin16-Aug-11 2:39 
GeneralRe: Why does this not change my string? Pin
stephen.darling16-Aug-11 7:27
stephen.darling16-Aug-11 7:27 
AnswerRe: Why does this not change my string? Pin
PIEBALDconsult15-Aug-11 15:30
mvePIEBALDconsult15-Aug-11 15:30 
AnswerRe: Why does this not change my string? [modified] Pin
Shameel15-Aug-11 21:33
professionalShameel15-Aug-11 21:33 
stephen.darling wrote:
It simply does not alter the string.

And it will not, no matter what you do. As others have already pointed out, strings are immutable in .NET which means they cannot be altered once they're created.

Take a look at this example:
C#
string a = "abc";
a = a + "xyz";

You might be thinking that in line 2 the characters "xyz" is appended to the string a, but that is not the case. When line 2 is executed, the Runtime creates a new string that is the result of the concatenation i.e. "abcxyz" and makes the variable a point to the new string. The old "abc" string becomes garbage (since it has no valid reference) and is collected during the next GC cycle.

To make large changes to to strings, it is recommended to use the System.Text.StringBuilder class.
C#
private string encodeString(string input)
{
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < input.Length; i++) {
        builder.Append(input[i]);
        if ((i + 1) % 6 == 0) {
            builder.Append("-");
        }
    }
    return builder.ToString();
}


And use your method like this:
C#
string input = "00000000000000000000";
string output = encodeString(input); //output contains "00000-00000-00000-00000"


modified on Tuesday, August 16, 2011 2:45 PM

GeneralRe: Why does this not change my string? Pin
Matt Meyer16-Aug-11 5:50
Matt Meyer16-Aug-11 5:50 
GeneralRe: Why does this not change my string? Pin
Shameel16-Aug-11 8:45
professionalShameel16-Aug-11 8:45 
QuestionFind asp.net listview control in Item Template Pin
akosidandan15-Aug-11 6:05
akosidandan15-Aug-11 6:05 
AnswerRe: Find asp.net listview control in Item Template Pin
akosidandan15-Aug-11 7:29
akosidandan15-Aug-11 7:29 
QuestionDeserialise complicated Jason data Pin
AndieDu14-Aug-11 15:55
AndieDu14-Aug-11 15:55 
AnswerRe: Deserialise complicated Jason data Pin
BobJanova14-Aug-11 23:18
BobJanova14-Aug-11 23:18 
GeneralRe: Deserialise complicated Jason data Pin
AndieDu15-Aug-11 14:42
AndieDu15-Aug-11 14:42 
AnswerRe: Deserialise complicated Jason data Pin
Bernhard Hiller17-Aug-11 1:00
Bernhard Hiller17-Aug-11 1:00 
QuestionC# MySQL Question Pin
d87c14-Aug-11 13:18
d87c14-Aug-11 13:18 
AnswerRe: C# MySQL Question Pin
Richard Andrew x6414-Aug-11 13:44
professionalRichard Andrew x6414-Aug-11 13:44 
GeneralRe: C# MySQL Question Pin
d87c14-Aug-11 15:33
d87c14-Aug-11 15:33 
GeneralRe: C# MySQL Question Pin
Richard Andrew x6414-Aug-11 15:39
professionalRichard Andrew x6414-Aug-11 15:39 
GeneralRe: C# MySQL Question Pin
d87c14-Aug-11 16:57
d87c14-Aug-11 16:57 
GeneralRe: C# MySQL Question Pin
PIEBALDconsult14-Aug-11 17:59
mvePIEBALDconsult14-Aug-11 17:59 
GeneralMessage Removed Pin
15-Aug-11 2:44
marssilen15-Aug-11 2:44 
GeneralRe: lame-ass hack attempt removed PinPopular
#realJSOP15-Aug-11 2:50
professional#realJSOP15-Aug-11 2:50 
GeneralRe: lame-ass hack attempt removed Pin
OriginalGriff15-Aug-11 3:05
mveOriginalGriff15-Aug-11 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.