user1173169
user1173169

Reputation:

Assign "empty" to a Stringbuilder object

Can anyone say me why when using this block of code :

   StringBuilder temp = new StringBuilder(strSource);

                for (int i = Start; i <= End-1; i++)
                {
                    temp[i] = '';
                }

I get an error in the "for" loop: literal empty character.

On the other hand, this works:

temp[i] = ' ';

Upvotes: 0

Views: 118

Answers (2)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

You're trying to reinvent the Remove method:

if (End > Start)
    temp.Remove(Start, End - Start);

'' is not valid because single quotes introduce a char literal, which must always be one char.

Upvotes: 3

SLaks
SLaks

Reputation: 887365

Characters must have a value.

Instead, you should write

temp.Length = 0;

Upvotes: 0

Related Questions