changing a character in a C# string

suppose I have created a string in C# and I want to change the last character to a blank.

private static string sOldMassiveOutput;
 
public static string oldmassiveoutput
 {
 get { return sOldMassiveOutput.Trim(); }
 set { sOldMassiveOutput = value; }
 }
 oldmassiveoutput[oldmassiveoutput.Length - 1] = ' ';

This will not work.
How do I make it work?

Recommended Answers

Strings are immutable objects. That is, whenever you make a "change" to a string, a new string is created, and you reference the new string.

To replace the last character:

oldmassiveoutput = oldmassiveoutput.Substring(0, oldmassiveoutput.Length - 1) + " ";
Jump to Post

All 2 Replies

Strings are immutable objects. That is, whenever you make a "change" to a string, a new string is created, and you reference the new string.

To replace the last character:

oldmassiveoutput = oldmassiveoutput.Substring(0, oldmassiveoutput.Length - 1) + " ";
commented: Or.. System.Text.StringBuilder :) +15

Or use the Remove method and append a space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.