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?
RosanneParnellAsked:
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.

mpf1748Commented:
Take a look at the Substring method of String.

HTH,
mpf1748
0
acharbonneauCommented:
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;
s.Substring(...);


In your case, use
s.Substring(0,2)

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

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

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:
Hello

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
      MessageBox.Show(s.Substring(0,2));
      // this will delete all the characters  except the first two
      s = s.Remove(s.Length-2,2);
      MessageBox.Show(ss);

Regards,
0
dsaboCommented:
You could use trim('3') or trim("3")
0
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
C#

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.