Solved

VS 2008 C# string trim and chr

Posted on 2010-11-19
6
692 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 75

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 75

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 75

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 75

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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