Solved

VS 2008 C# string trim and chr

Posted on 2010-11-19
6
689 Views
Last Modified: 2013-12-17
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);



                DataDataAdapter.Fill(DataDataSet);



                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"] + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|" + "|";

                    tw.WriteLine(DataString);

                }



                DataDataSet.Dispose();



                tw.Close();

Open in new window

0
Comment
Question by:rwheeler23
  • 4
6 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34175186
There are three functions available for trimming:

Trim
TrimStart
TrimEnd


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

   (char)166
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34175206
P.S.

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

Example:
// 1
string value = "hello    ";

value.TrimEnd();


// 2
"hello    ".TrimStart();

Open in new window

0
 
LVL 3

Expert Comment

by:iharding
ID: 34177035
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

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34177055
@iharding

Good call. I should have made note of that  :)
0
 

Author Comment

by:rwheeler23
ID: 34177923
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?
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34179900
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now