trimming a string in C#

I have a variable containing "MS33" as its contents. I want to trim the variable to only the first 2 characters. Result="MS". What function will do this for me?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Take a look at the Substring method of String.

Substring is the right method, but note that it's not static.
You can't call String.Substring(...)
You need to first create an instance of a string, then call the method from the object variable.

String s;

In your case, use

This returns the first two characters.
oops...need to initialize it also:

String s;
s = "MS33"
s.Substring(...);  //returns "MS"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mohammed NasmanSoftware DeveloperCommented:

If you would like just to return first two characters use Substring

but if you would like to trim the variable to contian only the first two characters use Remove method

      string s = "MS33";
      // this will return the first two characters without effect the variable
      // this will delete all the characters  except the first two
      s = s.Remove(s.Length-2,2);

You could use trim('3') or trim("3")
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.