For removing trailing part is I use the code
str.substring(0,str.length -2)
Is there a better way? I especially dislike using str.length.
Edit I wanted the question to be as short and simple as possible and I supposed that I just overlooked some String method. I try to be more precise.
I need to remove given number of characters from the end of a string. I don't want to care what the characters are and I don't want to introduce a risk of removing more characters.
If the number is larger then string length let's there is an exception (the string should be validated earlier).
I don't have the problem especially with length, but with referencing a variable twice (imagine a function instead of a variable). I also don't like necessity of subtraction but it is only a personal preference.
VB.NET soution
The question is taged vb.net so there is vb.net code (should be in a module):
<System.Runtime.CompilerServices.Extension> _
Public Shared Function RemoveFromEnd(stringValue As String, removedCharacters As Integer) As String
Return stringValue.Substring(0, stringValue.Length - removedCharacters)
End Function
If your concern is simply the aesthetics of what you're looking at, it is trivial to wrap this functionality in an extension method:
public static string RemoveFromEnd(this string stringValue, int removedCharacters)
{
return stringValue.Substring(0, stringValue.Length - removedCharacters);
}
So you could then simply use
str.RemoveFromEnd(2);
and be done with it.
If you are instead worried about the performance of using string.length due to its reputation for being slow in unmanaged languages, don't be. Since strings in .Net are immutable, String.Length is set when the string is declared and never changes. Therefore, referencing String.Length completes in constant time (e.g., Big-O(1)) and is very quick. By contrast, you should instead focus on the Substring call if you're sensitive to performance. In .Net, Substring completes in linear time (e.g., Big-O(n)) where n is the length of your original string. It is therefore the performance bottleneck in your line of code. It will become slower with respect to the length of the string.
If you're dealing with very long strings, though, it may not be OK to just swallow that. To get around something like this you would use a StringBuilder in your extension method instead:
public static string RemoveFromEnd(this string stringValue, int removedCharacters)
{
var newString = new StringBuilder(stringValue);
newString.Length = newString.Length - removedCharacters;
return newString.ToString();
}
This should be all constant time, but I need to double check Reflector to make sure StringBuilder.Length behaves the way I think it does. Even if it's linear time, though, it'll be linear with respect to removedCharacters, which is necessarily going to be lower than the length of the original string. The tradeoff, obviously, is that you have slightly more memory use until the StringBuilder goes out of scope, but that shouldn't be an issue if you wrap it in an extension method that completes really fast.
EDIT: Yes, StringBuilder.Length simply returns String.Length of the string it's keeping track of, so that returns in constant time and the StringBuilder approach to truncation should be as fast as it can reasonably be.
Check out this related question:
Trim string from the end of a string in .NET - why is this missing?
Since there is no built in method to do this, you'll need to make your own method (preferably an extension method):
public static string RemoveCharsFromEnd(this string input, int charactersToRemove)
{
if (input.Length > charactersToRemove)
return input.substring(0, input.Length - charactersToRemove);
else
return "";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With