Results 1 to 10 of 10

Thread: [RESOLVED] string builder question

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [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:
    1. Dim line As New System.Text.StringBuilder
    2.             Dim index As Integer
    3.  
    4.             If header = True Then
    5.  
    6.                 'Iterate over all the columns in the table and get the column names.
    7.                 For index = 0 To table.Columns.Count - 1 Step 1
    8.  
    9.                     If index > 0 Then
    10.                         'Put a comma between column names.
    11.                         line.Append(","c)
    12.                     End If
    13.  
    14.                     'Precede and follow each column name with a double quote
    15.                     'and escape each double quote with another double quote.
    16.                     line.Append(""""c)
    17.                     line.Append(table.Columns(index).ColumnName.Replace("""", """"""))
    18.                     line.Append(""""c)

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: string builder question

    Don't use the c to specify character
    Code:
    sb.Append(",");

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: string builder question

    well crap that was easy

    what does the c do anyway

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: string builder question

    The c tells visual basic that it is a character type, not string type.

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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?

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: string builder question

    line.Append("\""c);

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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.

  8. #8
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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#
    Show Appreciation. Rate Posts.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: string builder question

    In VB this is appending a Char object to a StringBuilder:
    VB Code:
    1. line.Append(","c)
    while this is appending a String object to a StringBuilder:
    VB Code:
    1. line.Append(",")
    In C# this is appending a Char object to a StringBuilder:
    Code:
    line.Append(',');
    while this is appending a String object to a StringBuilder:
    Code:
    line.Append(",");
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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
  •  



Click Here to Expand Forum to Full Width