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 7809929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:35:29+00:00 2026-06-02T03:35:29+00:00

Not often but sometimes I need to use String.Trim() to remove whitespaces of a

  • 0

Not often but sometimes I need to use String.Trim() to remove whitespaces of a string.
If it was a longer time since last trim coding I write:

string s = " text ";
s.Trim();

and be surprised why s is not changed. I need to write:

string s = " text ";
s = s.Trim();

Why are some string methods designed in this (not very intuitive) way? Is there something special with strings?

  • 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-02T03:35:32+00:00Added an answer on June 2, 2026 at 3:35 am

    s.Trim() creates a new trimmed version of the original string and returns it instead of storing the new version in s. So, what you have to do is to store the trimmed instance in your variable:

      s = s.Trim();
    

    This pattern is followed in all the string methods and extension methods.

    The fact that string is immutable doesn’t have to do with the decision to use this pattern, but with the fact of how strings are kept in memory. This methods could have been designed to create the new modified string instance in memory and point the variable to the new instance.

    It’s also good to remember that if you need to make lots of modifications to a string, it’s much better to use an StringBuilder, which behaves like a “mutable” string, and it’s much more eficient doing this kind of operations.

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

Sidebar

Related Questions

One problem I have with open-source is not often the product or documentation, but
I often commit files with similar cvs comment but not in a single operation.
I'm often changing files not from subversion, but from finder, or other sources. Recently,
I have a not-so-small class under development (that it changes often) and I need
I'm panning to a nearby location but the panTo function often jumps there since
I have a program that collects objects over time. Those objects are often, but
I have settings dialog with a number of ComboBoxes. More often than not, these
(Not related to versioning the database schema) Applications that interfaces with databases often have
I have often PATHs for files which do not exist in my codes. I
I have often heard complaints against Java for not having unsigned data types. See

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.