oshirowanen
oshirowanen

Reputation: 15925

String manipulation, removing last few characters

When I try to remove the last few characters of a string, I get an index out of range error. I am using the following to remove the characters from the end of the string:

objJSONStringBuilder.Remove(objJSONStringBuilder.Length - 1, 6)

The string has <hr /> at the end which I want to remove.

Upvotes: 0

Views: 1483

Answers (3)

J&#243;n Trausti Arason
J&#243;n Trausti Arason

Reputation: 4698

(StartIndex, Count from start index)

You're specifying that the start index should be the last character + 6 characters, that is 6 characters out of the index, thus out of bounds!

You'd rather want to do something like:

(length - 7, 6)

That would take the last 6 characters if there are 6 or more characters (or else you'll also get out of bounds)

Upvotes: 0

Sem Vanmeenen
Sem Vanmeenen

Reputation: 2151

The first parameter is the index from where you want to start removing. Use

objJSONStringBuilder.Remove(objJSONStringBuilder.Length - 6, 6)

Upvotes: 6

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

The count goes forwards, not backwards.

objJSONStringBuilder.Remove(objJSONStringBuilder.Length - 6, 6)

Upvotes: 2

Related Questions