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. c# literal conversion

c# literal conversion

Scheduled Pinned Locked Moved C#
csharpquestion
5 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.
  • C Offline
    C Offline
    Chris Rickard
    wrote on last edited by
    #1

    Does anyone know of a class that can convert a string in the c# string literal format to a regular .NET string and vice versa? I want to have a designable string property that allows special characters and I figure a TypeConverter that supports the c# string literal format is probably the easiest way to do it, unless there exists a better way? Thanks

    L 1 Reply Last reply
    0
    • C Chris Rickard

      Does anyone know of a class that can convert a string in the c# string literal format to a regular .NET string and vice versa? I want to have a designable string property that allows special characters and I figure a TypeConverter that supports the c# string literal format is probably the easiest way to do it, unless there exists a better way? Thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Maybe some examples of the required conversion would be helpful...

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      C 1 Reply Last reply
      0
      • L Luc Pattyn

        Maybe some examples of the required conversion would be helpful...

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        C Offline
        C Offline
        Chris Rickard
        wrote on last edited by
        #3

        Say you had a textbox with a property named "WhiteSpaceChars" of type string. Each char in the string is what should be a white space character. the default value for this string would be (in c# code) whiteSpaceChars = " \t\r\n"; Now I would like that string to show up in the property designer for users to be able to edit. Now obviously the default string TypeConverter in windows forms can't handle these particular characters. However if I had a TypeConverter that could handle the c# string literal syntax my problem would be solved. This goes both ways, if the user types in a value of \x00010 I would like the appropriate string returned to my class as if it were a string literal.

        L 1 Reply Last reply
        0
        • C Chris Rickard

          Say you had a textbox with a property named "WhiteSpaceChars" of type string. Each char in the string is what should be a white space character. the default value for this string would be (in c# code) whiteSpaceChars = " \t\r\n"; Now I would like that string to show up in the property designer for users to be able to edit. Now obviously the default string TypeConverter in windows forms can't handle these particular characters. However if I had a TypeConverter that could handle the c# string literal syntax my problem would be solved. This goes both ways, if the user types in a value of \x00010 I would like the appropriate string returned to my class as if it were a string literal.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, if your only concern is the one backslash/two backslashes issue, you can convert back and forth with a simple method. I dont think it is provided by .NET, so you would have to write it yourself (I'll use curly braces to indicate a single char): say input is ab\tc\nd then output would have to be ab{TAB}c{NEWLINE}d you can do sequential replacement of \t by {TAB}, then \n by {NEWLINE} etc using String.Replace the order is irrelevant, since old and new strings do not conflict with each other. similar method is required for the other direction. if you also want to replace the \x#### and \u#### character codes by their actual char, things become more complicated. Do you ? :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          C 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, if your only concern is the one backslash/two backslashes issue, you can convert back and forth with a simple method. I dont think it is provided by .NET, so you would have to write it yourself (I'll use curly braces to indicate a single char): say input is ab\tc\nd then output would have to be ab{TAB}c{NEWLINE}d you can do sequential replacement of \t by {TAB}, then \n by {NEWLINE} etc using String.Replace the order is irrelevant, since old and new strings do not conflict with each other. similar method is required for the other direction. if you also want to replace the \x#### and \u#### character codes by their actual char, things become more complicated. Do you ? :)

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            C Offline
            C Offline
            Chris Rickard
            wrote on last edited by
            #5

            Yeah I think you're right, a tagged expression would be much more easily parsed. Even a {x####} and {u####} could be easily extracted with some simple regexps. Thanks!

            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