[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

trimming a string in C#

Posted on 2004-11-18
5
Medium Priority
?
294 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:RosanneParnell
5 Comments
 
LVL 3

Expert Comment

by:mpf1748
ID: 12618589
Take a look at the Substring method of String.

HTH,
mpf1748
0
 
LVL 2

Expert Comment

by:acharbonneau
ID: 12618851
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
 
LVL 2

Accepted Solution

by:
acharbonneau earned 2000 total points
ID: 12618867
oops...need to initialize it also:

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

Expert Comment

by:Mohammed Nasman
ID: 12619113
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
 
LVL 2

Expert Comment

by:dsabo
ID: 12621570
You could use trim('3') or trim("3")
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question