Link to home
Start Free TrialLog in
Avatar of P1ST0LPETE
P1ST0LPETEFlag for United States of America

asked on

Truncating a String in C#

What is the proper syntax to truncate (reduce) a string in C#?

For example:
     myString = "SELECT * FROM table WHERE fieldname = variable AND"

and I want to cut off the last 3 characters in the string (the "AND") and make the new value be
    myString = "SELECT * FROM table WHERE fieldname = variable"

ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of P1ST0LPETE

ASKER

WOW!  Good solution.  However, I'd still like to know how to truncate a string :-)
angelll is correct. However, if you have no choice in the matter,
string myString = myString.SubString(0, myString.Length - 3);

will trim the last three characters.

Jim
Thanks Jim.
My pleasure. Good luck.

Jim