VS 2008 C# string trim and chr

I have two questions about about this snippet of code.
1) In my SQL select statement I used the rtrim() function. If I wanted to do the trimming in the code what are those functions in C#. I see examples of strings.trim but I try typing that into my code and it is not valid. Is there some reference I need to add to my project?
2) You the the pipe character (|) in the string. Is there a C# function equivalent to chr() so I could have typed chr(166) to get the pip character in the string?
SqlConnection DataConnection = GetConnection();

                /* Define record set for query results of inventory items */
                DataDataSet = new System.Data.DataSet();
                DataDataSet.CaseSensitive = false;

                DataCommand = new System.Data.SqlClient.SqlCommand();
                DataCommand.Connection = DataConnection;

                DataCommand.CommandText = "SELECT rtrim(IDInsurCompany) as IDInsurCompany,rtrim(PID) as PID,rtrim(IDPatInsur) as IDPatInsur,rtrim(FirstName) as Firstname,rtrim(LastName) as LastName,rtrim(convert(char(10),DOB,101)) as DOB from dmpatinsur order by IDPatInsur";

                DataDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                DataDataAdapter.SelectCommand = DataCommand;
                _commandBuilder = new SqlCommandBuilder(DataDataAdapter);


                dt = DataDataSet.Tables[0];
                foreach (DataRow dr_loopvariable in dt.Rows)
                    dr = dr_loopvariable;
                    DataString = "|" + dr["IDInsurCompany"] + "|" + dr["PID"] + "|" + dr["IDPatInsur"] + "|" + "|" + "|" + dr["FirstName"] + "|" + "|" + dr["LastName"] + "|" + "|" + dr["DOB"] + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|";



Open in new window

Who is Participating?

Improve company productivity with a Business Account.Sign Up

käµfm³d 👽Connect With a Mentor Commented:
Negative. Trim() will trim starting and ending spaces, not all spaces. You would need to call Replace() to remove all spaces.

The @ symbol makes a string raw input--backslashes are no longer escape characters, so "\t" would print as "\t" and not an actual tab character.
käµfm³d 👽Commented:
There are three functions available for trimming:


You can cast the numeric value 166 to a char type and you should get the pipe:

käµfm³d 👽Commented:

You have to call the Trim family of functions on a valid instance of a string and you shouldn't need to reference any DLL by default. Also, remember that C# is case-sensitive, so while TrimStart() is a valid function, trimstart() is not  :)

// 1
string value = "hello    ";


// 2
"hello    ".TrimStart();

Open in new window

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Also, please also note, that the Trim, TrimEnd and TrimStart methods return a string.  They do NOT modify original string.  The following code will have the results of:

test = "Hello World! |  <= trailing spaces.
trimmed = "Hello World!" <= no spaces.

string test = @"Hello World! |  ";
string trimmed = test.Trim();

Open in new window

käµfm³d 👽Commented:

Good call. I should have made note of that  :)
rwheeler23Author Commented:
string testH = @"Hello           "
string testW = @"World! |  "
string trimmed = testH.Trim() + " " + testW.Trim();

so trimmed would equal  "Hello World!"   No spaces between Hello and World!, correct?

By the way, what does the @ sign do?
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.

All Courses

From novice to tech pro — start learning today.