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 with escaped squences...

String with escaped squences...

Scheduled Pinned Locked Moved C#
visual-studiotutorialquestion
11 Posts 5 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.
  • E Offline
    E Offline
    e laj
    wrote on last edited by
    #1

    Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

    A C A E R 7 Replies Last reply
    0
    • E e laj

      Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

      A Offline
      A Offline
      Alaric_
      wrote on last edited by
      #2

      *simple* solution....as you asked. If you know every one of your escape characters (say you only use 2 for illustrative purposes... '/t' and '/n') you can add each one of them to a string array. Then whenever you need to parse your input string, call inputString.Split(delimiterList) where delimiterList is your array of characters. That parses your text as you requested, but I don't think you really want what you asked. ...If you allow users to insert escape characters, why would you want to eliminate them whenever you parse the input string? I think what you are actually asking is for an algorithm to perform character substitution for a particular escape sequence. Am I right?

      1 Reply Last reply
      0
      • E e laj

        Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Escape characters are in your text when you enter the constant, but in the string itself, they become ( for example ) tabs and so on. If you have a string which actually contains the \t, etc, then you need to use string.Replace to remove them, if that's what you need.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        1 Reply Last reply
        0
        • E e laj

          Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

          A Offline
          A Offline
          Alaric_
          wrote on last edited by
          #4

          Please give a more illustrative example of what you are trying to accomplish. Are you saying that '\t' is supposed to represent a single whitespace character? In the original reply that I made, I assumed that 'hello' and 'there' were 2 seperate tokens created from parsing a string with a given delimiter. I think you really meant that you want to traverse the string and perform a character substitution based upon your defined escape sequences.

          1 Reply Last reply
          0
          • E e laj

            Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

            A Offline
            A Offline
            AndriyZZ
            wrote on last edited by
            #5

            Hello This is what you need: string s1 = "hello\tthere".Replace("\\t", "\t"); string s2 = "hello\tthere".Replace("\t", "\\t"); Andriy Zhornyk, regards

            1 Reply Last reply
            0
            • E e laj

              Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

              E Offline
              E Offline
              e laj
              wrote on last edited by
              #6

              The idea is to enable the user to enter string constants that contains escape character - eaxactly as we do when we edit our source code in VS. For example, the user maybe enter the following text in some text box: "Hello\there\nand\there. But \\this is a backslash and not a tab chracater" So, when i get this string, .NET framework treats these sequences as is and not as escape character - this is the expected behavior. If you add a breakpoint at the right point and watch this user input in the debugger you will see: "Hello\\there\\nand\\there. But \\\\this is a backslash and not a tab chracater" However, i was wondering if there is a simple way, that is method supplied by the framework that do the job of escape character substitution as well as the reverse job. So when i get this string i do the following: string result = SubstituteEscapes(userInput); ...and the result will contains the same string after the "\t" replaced by the tab character and the "\n" replaced by the new line character and the "\\" will be replaced by a single "\". and the reverse way should be: string original = SubstituteSpecialCharacters(result); ...and the the tab, new line and backslash characters will be replaced with the appropriate escape sequences. It is possible to search and replace those escapes even by using regualr expressions. But does the framework has a built in method for that? Why? You may ask your self why i need these methods? Well i use a ProperttyGrid and ask the user what should be the seperator string between fields in the log file. Beacause pressing tab move the focus to the next property in the grid, and also because copying and pasting tab is BAD solution to this problem, i want to give the user the possiblity to enter escapes. Thanks, again!

              C 1 Reply Last reply
              0
              • E e laj

                The idea is to enable the user to enter string constants that contains escape character - eaxactly as we do when we edit our source code in VS. For example, the user maybe enter the following text in some text box: "Hello\there\nand\there. But \\this is a backslash and not a tab chracater" So, when i get this string, .NET framework treats these sequences as is and not as escape character - this is the expected behavior. If you add a breakpoint at the right point and watch this user input in the debugger you will see: "Hello\\there\\nand\\there. But \\\\this is a backslash and not a tab chracater" However, i was wondering if there is a simple way, that is method supplied by the framework that do the job of escape character substitution as well as the reverse job. So when i get this string i do the following: string result = SubstituteEscapes(userInput); ...and the result will contains the same string after the "\t" replaced by the tab character and the "\n" replaced by the new line character and the "\\" will be replaced by a single "\". and the reverse way should be: string original = SubstituteSpecialCharacters(result); ...and the the tab, new line and backslash characters will be replaced with the appropriate escape sequences. It is possible to search and replace those escapes even by using regualr expressions. But does the framework has a built in method for that? Why? You may ask your self why i need these methods? Well i use a ProperttyGrid and ask the user what should be the seperator string between fields in the log file. Beacause pressing tab move the focus to the next property in the grid, and also because copying and pasting tab is BAD solution to this problem, i want to give the user the possiblity to enter escapes. Thanks, again!

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Your users are sufficiently technical to understand the concept ? So you actually want to parse for \t and replace it with a tab ? I imagine you need to use the string.Replace method to do this for all known special characters, including replacing \\ with \

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                E 1 Reply Last reply
                0
                • C Christian Graus

                  Your users are sufficiently technical to understand the concept ? So you actually want to parse for \t and replace it with a tab ? I imagine you need to use the string.Replace method to do this for all known special characters, including replacing \\ with \

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  E Offline
                  E Offline
                  e laj
                  wrote on last edited by
                  #8

                  Well, they are. However it seems up to this point that there is no such built in method, hence i am writing such method now. The principle is a little more complicated than seraching and replacing, consider the \uxxxx unicode escapes, \xd[d][d][d] hexadecimal escapes (variable length up to 4 digits) etc. At the moment i finish this method (soon) i post it.

                  1 Reply Last reply
                  0
                  • E e laj

                    Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

                    E Offline
                    E Offline
                    e laj
                    wrote on last edited by
                    #9

                    First, thank you all for trying to help me, as i said, it seems that there is no built in method for escape sequences substitution. Hence i set down and wrote the following method, hopefully with no bugs. In the method i use scanning instead of serching and replacing as well as some bitwise operations in order to enhance performance. The reverse method is much simpler to code. Finally, here in Israel we celebrate a new year - so happy new year to you all. Here is the method. /// /// Substitutes the escape characters in the specified string with their appropriate characters. /// /// The string. /// The string after the escape characters were replaced by the appropriate character. /// /// If the method encounter invalid escape character, the backslash character is removed and /// the character that comes right after it is copied with no change. /// public static string SubstituteEscapes(string str) { StringBuilder sbResult = new StringBuilder(); bool bEscaped = false; for(int i = 0; i < str.Length; i++) { char current = str[i]; if(bEscaped) { switch(current) { case '\'': case '\"': case '\\': /* Merely append the current character. This is the default behavior */ sbResult.Append(current); break; case '0': /* The '\0' (string terminator) character \u0000 */ sbResult.Append('\0'); break; case 'a': /* The bell (alarm) character \u0007*/ sbResult.Append('\a'); break; case 'b': /* The backspace character \u0008*/ sbResult.Append('\b'); break; case 'f': /* The form feed character \u000C*/ sbResult.Append('\f'); break; case 'n': /* The line feed character \u000A*/ sbResult.Append('\n'); break; case 'r': /* The carriage return character \u000D*/ sbResult.Append('\r'); break; case 't': /* The tab character \u0009*/ sbResult.Append('\t'); break; case 'v': /* The vertical tab character \u000B*/ sbResult.Append('\v'); break; case 'u': /* Unicode character (exactly four digits to read on) */ try { char ch = (char)int.Parse(str.Substring(i + 1, 4), NumberStyles.AllowHexSpecifier); sbResult.Append(ch);

                    1 Reply Last reply
                    0
                    • E e laj

                      Is there a simple way to parse a string that contains escape character to the appropriate string, and vise versa? For example, given the following string: "hello\tthere" should be parsed to: "hello there" and vs. I need these methods because i let the user to enter escape chracters within the strings. Thnaks!

                      R Offline
                      R Offline
                      Rob Graham
                      wrote on last edited by
                      #10

                      string WildCardPatern.UnEscape(string pattern) "Converts a string that has escaped characters to a string that includes the escaped characters in their unescaped form. " I've never tried it, but the description sounds like exactly what you want... Ooops, looks like that's only available in .Net 3.0 (Winfx) - System.Management.Automation namespace ...

                      E 1 Reply Last reply
                      0
                      • R Rob Graham

                        string WildCardPatern.UnEscape(string pattern) "Converts a string that has escaped characters to a string that includes the escaped characters in their unescaped form. " I've never tried it, but the description sounds like exactly what you want... Ooops, looks like that's only available in .Net 3.0 (Winfx) - System.Management.Automation namespace ...

                        E Offline
                        E Offline
                        e laj
                        wrote on last edited by
                        #11

                        After wasting some time writing the method, Rob Graham give me a clue. Rob Graham, you are right this method is in .NET 3.0, but there is another method with the same name: Regex.Unescape This function do the job. BUT, this method is not error tolerant like my method;P. So, if someone meant to use unescape and want to skip invalid escape sequences - my method does it. Rob Graham - Thank you, again!

                        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