Solved

VS 2008 C# string trim and chr

Posted on 2010-11-19
6
691 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 50
Need a complete list of ASP.NET page load events 3 26
VB.Net How to Exit Sub - Exit Form??? 5 50
Entity Framework 7 31
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

20 Experts available now in Live!

Get 1:1 Help Now