Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:52:29+00:00 2026-06-05T17:52:29+00:00

For removing trailing part is I use the code str.substring(0,str.length -2) Is there a

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:52:30+00:00Added an answer on June 5, 2026 at 5:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way (in .NET) of removing trailing zeros from a number when
After removing hash from URL using window.location.hash='' page getting reloaded in firefox. EDIT Example:
I want to remove trailing white spaces and tabs from my code without removing
I've got a pretty compact way of removing trailing zeros in decimal values but
I'm having difficulties removing the lineStyle lines in JTrees using Netbeans. In a standalone
I'm removing a component from the WindowedApplication, using removeChildAt(), and want to play an
I got into the habit of removing trailing white spaces from my source file.
What is the (or is there an) idiomatic way to strip newlines from strings
I currently use this piece of code to reduce a given text to a
I'm using .htaccess mod_rewrite for nicer URLs. Here is a part of my .htaccess

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.