Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String formatting question

String formatting question

Scheduled Pinned Locked Moved C#
questionhardware
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BrcKcc
    wrote on last edited by
    #1

    I want to format a string with embedded backslashes. The string that I want is as follows: This is John I\. Smith. Here's the code snippet: string temp1 = "John I\\. Smith"; string temp2 = String.Format("This is {0}.", temp); What I get in temp2 is: This is John I\\. Smith. instead of This is John I\. Smith. How do I get rid of the extra backslash? BRCKCC

    A 1 Reply Last reply
    0
    • B BrcKcc

      I want to format a string with embedded backslashes. The string that I want is as follows: This is John I\. Smith. Here's the code snippet: string temp1 = "John I\\. Smith"; string temp2 = String.Format("This is {0}.", temp); What I get in temp2 is: This is John I\\. Smith. instead of This is John I\. Smith. How do I get rid of the extra backslash? BRCKCC

      A Offline
      A Offline
      Andy M
      wrote on last edited by
      #2

      BrcKcc wrote: This is John I\\. Smith. Is that in the debugger, or in actual application output?? One easy way to be sure is to use string myString = @"Use\a\slash\only\once"; the @ symbol in front of the string literal tells the compiler not to interpret the slashes as escape sequences. If you do want to include an escape sequence this won't work for you. Cheers, Andy.

      B 1 Reply Last reply
      0
      • A Andy M

        BrcKcc wrote: This is John I\\. Smith. Is that in the debugger, or in actual application output?? One easy way to be sure is to use string myString = @"Use\a\slash\only\once"; the @ symbol in front of the string literal tells the compiler not to interpret the slashes as escape sequences. If you do want to include an escape sequence this won't work for you. Cheers, Andy.

        B Offline
        B Offline
        BrcKcc
        wrote on last edited by
        #3

        It was in the debugger. It turns out when I displayed the string via MessageBox.Show(), it only had one backslash. So thanks for that clarification. The @ symbol has been confusing. How do I convert a string defined with the @ symbol to a string without the @ symbol? Do you know where Microsoft documented this capability? Thanks. - Bruce BRCKCC

        A 1 Reply Last reply
        0
        • B BrcKcc

          It was in the debugger. It turns out when I displayed the string via MessageBox.Show(), it only had one backslash. So thanks for that clarification. The @ symbol has been confusing. How do I convert a string defined with the @ symbol to a string without the @ symbol? Do you know where Microsoft documented this capability? Thanks. - Bruce BRCKCC

          A Offline
          A Offline
          Andy M
          wrote on last edited by
          #4

          BrcKcc wrote: How do I convert a string defined with the @ symbol to a string without the @ symbol? What do you mean? They are all the same thing: a string object. string a = "String\\Literal"; string b = @"String\Literal"; a==b [EDIT - sorry, I had the slashes the wrong way round before] Cheers, Andy

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups