Search code examples
c#javastringbuffer

What's the equivalent C# method to this basic Java method?


Really simple question here (more to confirm my thoughts, than anything)...

Java method: StringBuffer.Delete(start,end);

Java code:

sb.delete(sb.length()-2, sb.length());  

C# (not sure if this is right):

StringBuilder sb = new StringBuilder();
....
sb.Remove(sb.Length - 2, sb.Length - (sb.Length - 2));

The reason why I'm not sure is in the documentation of the Java delete method. It says

The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists

I'm just not too sure about this end - 1 bit of that quote .. and if I might have fraked things up.

cheers :)

edit: Heh. I knew that it was deleting the last 2 chars from the string, but I was keeping the conversion exact, hence my verbose code. :)


Solution

  • To delete the last 2 characters you'd write:

    sb.Remove(sb.Length - 2, 2);