|
-
Sep 28th, 2006, 12:17 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] string builder question
I have a few lines i borrowed from the codebank here for my orginal vb project
i am having trouble converting it over to c#
in partular the line line.Append(","c)
im just not sure what the c is doing ive been reading through the stringbuilder on msdn but im still not sure what the eqivlancy in c# should be
can anyone help point me in the right direction
thanks
VB Code:
Dim line As New System.Text.StringBuilder
Dim index As Integer
If header = True Then
'Iterate over all the columns in the table and get the column names.
For index = 0 To table.Columns.Count - 1 Step 1
If index > 0 Then
'Put a comma between column names.
line.Append(","c)
End If
'Precede and follow each column name with a double quote
'and escape each double quote with another double quote.
line.Append(""""c)
line.Append(table.Columns(index).ColumnName.Replace("""", """"""))
line.Append(""""c)
-
Sep 28th, 2006, 12:37 PM
#2
Re: string builder question
Don't use the c to specify character
-
Sep 28th, 2006, 03:04 PM
#3
Thread Starter
Fanatic Member
Re: string builder question
well crap that was easy
what does the c do anyway
-
Sep 28th, 2006, 03:39 PM
#4
Re: string builder question
The c tells visual basic that it is a character type, not string type.
-
Sep 28th, 2006, 04:54 PM
#5
Thread Starter
Fanatic Member
Re: string builder question
if i use the line
line.Append(""""c)
i get ") expected"
but if i put in a ) i get a invaild expression term
any ideas?
-
Sep 28th, 2006, 05:03 PM
#6
Re: string builder question
-
Sep 28th, 2006, 05:31 PM
#7
Thread Starter
Fanatic Member
Re: string builder question
not that im not greatfull for the answer
could you explain it to me a little so i understand what the \ is for?
so i can apply it to this line
line.Append(table.Columns(index).ColumnName.Replace("\"", """"""))
------------------------------------------------------------^
Here
also shouldnt it be
line.Append("\"");
Last edited by Crash893; Sep 28th, 2006 at 05:34 PM.
-
Sep 28th, 2006, 05:36 PM
#8
Re: string builder question
\ in C#, C++, and C (or maybe some other languages), when followed by some specific character to result in some meaningful string, is known as Escape Sequence.
What character escape sequences are available in C#
-
Sep 28th, 2006, 05:49 PM
#9
Re: string builder question
In VB this is appending a Char object to a StringBuilder:while this is appending a String object to a StringBuilder:In C# this is appending a Char object to a StringBuilder:while this is appending a String object to a StringBuilder:VB does NOT support C-style escape sequences in strings, which is why you have to use concatenation to insert a line break in a string in VB. The ONLY character that requires escaping in VB is the double quote, to indicate that it is a literal character and not the end of the string. In VB a literal double quote gets escaped with another double quote, i.e. for every one double quote you want to appera in your string you have to input two double quotes.
C# DOES support C-style escape sequences, being based on C syntax. That means that any special characters in a C# string must be escaped with a backslash to indicate that they are special. These include a tab (\t), a carriage return (\r) and a line feed (\n). Because the backslash itself is a special character it must also be escaped with a backslash when you want it to appear as a literal character, i.e. for every one backslash you want to appear in your string you have to input two backslashes. C# has the same issue that VB does with the double quote in that it would normally indicate the end of the string, so if you want a literal double quote to appear in your string you must escape it, but in C# strings all escaping is done with the backslash.
Note that C# also supports the verbatim string operator, which tells the compiler to treat every character in the string as a literal. This is most useful for paths. Normally you'd have to write a path like this:
Code:
string myPath = "C:\\Folder\\Subfolder\\MyFile.txt";
with all the delimiters escaped, but C# allows you to do this:
Code:
string myPath = @"C:\Folder\Subfolder\MyFile.txt";
Because the verbatim string operator says that all characters are literal you cannot include ANY characters that require escaping, which includes double quotes.
-
Sep 28th, 2006, 06:40 PM
#10
Thread Starter
Fanatic Member
Re: string builder question
so a \" = ""
here is what i came up with
line.Append(table.Columns(index).ColumnName.Replace("\"", "\"\""));
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|