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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:46:03+00:00 2026-05-16T11:46:03+00:00

I have the following input: string txt = i am a string I want

  • 0

I have the following input:

string txt = "                   i am a string                                    "

I want to remove space from start of starting and end from a string.

The result should be: "i am a string"

How can I do this in c#?

  • 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-05-16T11:46:03+00:00Added an answer on May 16, 2026 at 11:46 am

    String.Trim

    Removes all leading and trailing white-space characters from the current String object.

    Usage:

    txt = txt.Trim();
    

    If this isn’t working then it highly likely that the “spaces” aren’t spaces but some other non printing or white space character, possibly tabs. In this case you need to use the String.Trim method which takes an array of characters:

      char[] charsToTrim = { ' ', '\t' };
      string result = txt.Trim(charsToTrim);
    

    Source

    You can add to this list as and when you come across more space like characters that are in your input data. Storing this list of characters in your database or configuration file would also mean that you don’t have to rebuild your application each time you come across a new character to check for.

    NOTE

    As of .NET 4 .Trim() removes any character that Char.IsWhiteSpace returns true for so it should work for most cases you come across. Given this, it’s probably not a good idea to replace this call with the one that takes a list of characters you have to maintain.

    It would be better to call the default .Trim() and then call the method with your list of characters.

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

Sidebar

Related Questions

Hi I have the following date as String format. Input 2010-04-20 05:34:58.0 Output I
Lets say I have the following: $('input[rel]').jOverlay({ overlayId: #overlayID }); How can I dynamically
I have the following: C:\temp\dowork.exe < input.txt processing....... complete C:\ I try this: processArguments
I have the following regular expression validator to detect whether an input string contains
I have the following function DateTime fromDateParam = DateTime.ParseExact(Convert.ToString(DateTime.MinValue),dd.MM.yyyy HH:mm:ss,null); It says input string
I have the following code: string input = ç; string normalized = input.Normalize(NormalizationForm.FormD); char[]
I have posted the following code where I am reading from an input file
I have the following code: void readFile(BST *tree) { ifstream infile(input.txt); long int sid,
i have the following file named asmfile.txt copy start 1000 read ldx zero rd
i've got a long string which is space delimited (read in from a txt

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.