Is there a better way to remove the end of a string in .NET?

To remove the end part, 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 assumed that I just forgot about some String method. I try to be more precise.

I need to remove a given number of characters from the end of a line. I do not want to care about what the characters are, and I do not want to introduce the risk of deleting more characters.

If the number is greater than the length of the string, let an exception exist (the string must be checked before).

I have no problem, especially with the length, but with a reference to the variable twice (imagine a function instead of a variable). I also do not like the need for subtraction, but this is only a personal preference.

VB.NET soution

Question about taged vb.net, so there is vb.net code (should be in the 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
+5
source share
5 answers

If your problem is just the aesthetics of what you are 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 can just use

    str.RemoveFromEnd(2);

and run with it.

, string.length - , . .Net , String.Length , ​​ . , String.Length (, Big-O (1)) . , , . .Net (, Big-O (n)), n - . . .

, , , . - , StringBuilder :

    public static string RemoveFromEnd(this string stringValue, int removedCharacters)
    {
        var newString = new StringBuilder(stringValue);
        newString.Length = newString.Length - removedCharacters;
        return newString.ToString();
    }

, Reflector, , StringBuilder.Length , . , , . , , , StringBuilder , , , .

EDIT: , StringBuilder.Length String.Length , , StringBuilder , .

+8

:

.NET - ?

, ( ):

public static string RemoveCharsFromEnd(this string input, int charactersToRemove) 
{
    if (input.Length > charactersToRemove) 
        return input.substring(0, input.Length - charactersToRemove);
    else
        return "";
}
+5

, 2 str.Length,

str.Substring(2)

, ? , .

str.Remove(str.Length - 2)

Length.

, Linq :

new string(str.Reverse().Skip(2).Reverse().ToArray())

, .

+3
source

It depends on what you mean by the back. Do you mean spaces after the content? For example:

string test = "This is a test string.   "; // But you want "This is a test string."

and you want to remove spaces after? This is possible with .Trim()or .TrimEnd(). If you do not mean this, indicate what the problem is.

0
source
        var test = " test";
        // Trim all trailing spaces
        test = test.Trim();
        // Trim only starting trailing spaces
        test = test.TrimStart();
        // Trim only ending trailing spaces
        test = test.TrimEnd();


        var test = "abcTest";
        // Trim all trailing abc
        var toRemove = new char[3];
        toRemove[0] = 'a';
        toRemove[1] = 'b';
        toRemove[2] = 'c';
        test = test.TrimStart(toRemove);

You must use the Trim () function to remove the end part.

0
source

All Articles