Solved

VS 2008 C# string trim and chr

Posted on 2010-11-19
6
693 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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