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. special character replacement

special character replacement

Scheduled Pinned Locked Moved C#
question
17 Posts 7 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.
  • V varsh12

    hi how can i replace "\\" with "\". because i am using, String.Replace("\\","\"); but it does not work. thanx

    K Offline
    K Offline
    Keith Barrow
    wrote on last edited by
    #3

    Normally, you don't need to:

    Console.WriteLine("\\");

    will output \. You have to do extra work to switch the escaping functionality off (using @ before the string):

    Console.WriteLine(@"\\");

    will output \\. If you are still having problems, please post extra code so we can see what is going on better.

    ragnaroknrol The Internet is For Porn[^]
    Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

    V 1 Reply Last reply
    0
    • K Keith Barrow

      Normally, you don't need to:

      Console.WriteLine("\\");

      will output \. You have to do extra work to switch the escaping functionality off (using @ before the string):

      Console.WriteLine(@"\\");

      will output \\. If you are still having problems, please post extra code so we can see what is going on better.

      ragnaroknrol The Internet is For Porn[^]
      Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

      V Offline
      V Offline
      varsh12
      wrote on last edited by
      #4

      thanks, but this is not work

      K 1 Reply Last reply
      0
      • V varsh12

        thanks, but this is not work

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #5

        The code I added does work, I checked it, and it is part of the language specification! If you want to output \ you need to use "\\" or @"\" If you want to output \\ you need to use "\\\\" or @"\\" The @ character switches the escaping off. If you are still having problems, I suggest you post more of your code, something else might be happening!

        ragnaroknrol The Internet is For Porn[^]
        Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

        1 Reply Last reply
        0
        • V varsh12

          hi how can i replace "\\" with "\". because i am using, String.Replace("\\","\"); but it does not work. thanx

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #6

          Is that your actual code? working example...

          string s = @"some\\text";
          s = s.Replace(@"\\", @"\");
          //s should now = "some\text";

          Life goes very fast. Tomorrow, today is already yesterday.

          V 1 Reply Last reply
          0
          • M musefan

            Is that your actual code? working example...

            string s = @"some\\text";
            s = s.Replace(@"\\", @"\");
            //s should now = "some\text";

            Life goes very fast. Tomorrow, today is already yesterday.

            V Offline
            V Offline
            varsh12
            wrote on last edited by
            #7

            Actually i am using xml file. In my xml file contains following text: \u092A\u094B\u0937\u093E\u0939\u093E\u0930 and i want to convert in correspponding letter through httpUtility.htmlEncode(). when i put above code in htmlEncode function, it works properly. when i retrieve this in C# as string it shows like: \\u092A\\u094B\\u0937\\u093E\\u0939\\u093E\\u0930. so this is not convert. please guide me.

            P 1 Reply Last reply
            0
            • V varsh12

              hi how can i replace "\\" with "\". because i am using, String.Replace("\\","\"); but it does not work. thanx

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #8

              Why do you need to?

              V 1 Reply Last reply
              0
              • V varsh12

                Actually i am using xml file. In my xml file contains following text: \u092A\u094B\u0937\u093E\u0939\u093E\u0930 and i want to convert in correspponding letter through httpUtility.htmlEncode(). when i put above code in htmlEncode function, it works properly. when i retrieve this in C# as string it shows like: \\u092A\\u094B\\u0937\\u093E\\u0939\\u093E\\u0930. so this is not convert. please guide me.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #9

                If you are simply looking at the value in the debugger, then those extra back-slashes aren't really there, don't worry about them.

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  Why do you need to?

                  V Offline
                  V Offline
                  varsh12
                  wrote on last edited by
                  #10

                  i WANT TO RETRIEVE EQUIVALENT VALUE OF THE GIVEN STRING. THE ABOVE CODE VALUE IN SEE IN HINDI IS "POSHAHAR".

                  P 1 Reply Last reply
                  0
                  • V varsh12

                    i WANT TO RETRIEVE EQUIVALENT VALUE OF THE GIVEN STRING. THE ABOVE CODE VALUE IN SEE IN HINDI IS "POSHAHAR".

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #11

                    OW! My eyes!

                    K 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      OW! My eyes!

                      K Offline
                      K Offline
                      Keith Barrow
                      wrote on last edited by
                      #12

                      And ears...... It turns out the code I posted demonstrating the difference between

                      Console.WriteLine("\\");
                      Console.WriteLine(@"\\");

                      doesn't work. There goes ~10 years of .net experience perhaps I should go back to asking people to switch it off and on again for a living :-)

                      ragnaroknrol The Internet is For Porn[^]
                      Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                      1 Reply Last reply
                      0
                      • V varsh12

                        hi how can i replace "\\" with "\". because i am using, String.Replace("\\","\"); but it does not work. thanx

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

                        ahmad25 wrote:

                        it does not work

                        that is not informative at all. if you want a good answer, then provide quality information to begin with. Don't say "it does not work", specify what kind of problem there is (compilation error, run-time error, ...) and be specific (error number, exception message, line number, etc). :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                        V 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          ahmad25 wrote:

                          it does not work

                          that is not informative at all. if you want a good answer, then provide quality information to begin with. Don't say "it does not work", specify what kind of problem there is (compilation error, run-time error, ...) and be specific (error number, exception message, line number, etc). :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                          V Offline
                          V Offline
                          varsh12
                          wrote on last edited by
                          #14

                          this is my string: \u092A\u094B\u0937\u093E\u0939\u093E\u0930 i used this code:

                          string unicodeString = HttpUtility.HtmlEncode("\u092A\u094B\u0937\u093E\u0939\u093E\u0930");
                          MessageBox.Show(unicodeString);

                          it gives me answer correct as "POSHAHAR" IN HINDI FONT. BUT if we use above text as string it gives

                          string unicodeString = HttpUtility.HtmlEncode(above_code);
                          MessageBox.Show(unicodeString);

                          the string takes value in this form: "\\u092A\\u094B\\u0937\\u093E\\u0939\\u093E\\u0930";

                          so, it can't work. then my problem is , how am i replace "\\" instead of "\".

                          S L 2 Replies Last reply
                          0
                          • V varsh12

                            this is my string: \u092A\u094B\u0937\u093E\u0939\u093E\u0930 i used this code:

                            string unicodeString = HttpUtility.HtmlEncode("\u092A\u094B\u0937\u093E\u0939\u093E\u0930");
                            MessageBox.Show(unicodeString);

                            it gives me answer correct as "POSHAHAR" IN HINDI FONT. BUT if we use above text as string it gives

                            string unicodeString = HttpUtility.HtmlEncode(above_code);
                            MessageBox.Show(unicodeString);

                            the string takes value in this form: "\\u092A\\u094B\\u0937\\u093E\\u0939\\u093E\\u0930";

                            so, it can't work. then my problem is , how am i replace "\\" instead of "\".

                            S Offline
                            S Offline
                            Sauro Viti
                            wrote on last edited by
                            #15

                            It is not as you said: in C# each string is unicode and something like "\u092A" is an escape sequence that mean "the unicode character with the code 0x092A. In other words, it is not a sequence of characters, but a single one.

                            1 Reply Last reply
                            0
                            • V varsh12

                              this is my string: \u092A\u094B\u0937\u093E\u0939\u093E\u0930 i used this code:

                              string unicodeString = HttpUtility.HtmlEncode("\u092A\u094B\u0937\u093E\u0939\u093E\u0930");
                              MessageBox.Show(unicodeString);

                              it gives me answer correct as "POSHAHAR" IN HINDI FONT. BUT if we use above text as string it gives

                              string unicodeString = HttpUtility.HtmlEncode(above_code);
                              MessageBox.Show(unicodeString);

                              the string takes value in this form: "\\u092A\\u094B\\u0937\\u093E\\u0939\\u093E\\u0930";

                              so, it can't work. then my problem is , how am i replace "\\" instead of "\".

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

                              Hi, much better now, I understand you happen to have strings that hold Unicode characters in their \u format (as accepted by the C# compiler), and need them to be interpreted. I wasn't able to locate a .NET class that really supports this, so the best I could come up with is:

                              static string DecodeUnicode(string s) {
                              	StringBuilder sb=new StringBuilder();
                              	while (s.Length!=0) {
                              		sb.Append((char)int.Parse(s.Substring(2, 4), NumberStyles.HexNumber));
                              		s=s.Substring(6);
                              	}
                              	return sb.ToString();
                              }
                              

                              which only accepts strings that consist of groups of six characters (a backslash, a 'u', and 4 hex digits); you may choose to add checks and error handling. It works for your example.

                              string s="\\\\u092A\\\\u094B\\\\u0937\\\\u093E\\\\u0939\\\\u093E\\\\u0930";
                              log(s);
                              s=DecodeUnicode(s);
                              log(s);
                              

                              FWIW: I think you got yourself into trouble; most of the time, you should simply avoid having to do this, and have the compiler translate those \uXXXX sequences into single characters! PS: I'm not sure what HttpUtility.HtmlEncode is doing for you. :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                              V 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Hi, much better now, I understand you happen to have strings that hold Unicode characters in their \u format (as accepted by the C# compiler), and need them to be interpreted. I wasn't able to locate a .NET class that really supports this, so the best I could come up with is:

                                static string DecodeUnicode(string s) {
                                	StringBuilder sb=new StringBuilder();
                                	while (s.Length!=0) {
                                		sb.Append((char)int.Parse(s.Substring(2, 4), NumberStyles.HexNumber));
                                		s=s.Substring(6);
                                	}
                                	return sb.ToString();
                                }
                                

                                which only accepts strings that consist of groups of six characters (a backslash, a 'u', and 4 hex digits); you may choose to add checks and error handling. It works for your example.

                                string s="\\\\u092A\\\\u094B\\\\u0937\\\\u093E\\\\u0939\\\\u093E\\\\u0930";
                                log(s);
                                s=DecodeUnicode(s);
                                log(s);
                                

                                FWIW: I think you got yourself into trouble; most of the time, you should simply avoid having to do this, and have the compiler translate those \uXXXX sequences into single characters! PS: I'm not sure what HttpUtility.HtmlEncode is doing for you. :)

                                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                                V Offline
                                V Offline
                                varsh12
                                wrote on last edited by
                                #17

                                thank u very much,Luc Pattyn Sir!! u r really solved my problem and i m very happy. I always remember your suggestion. thanx. sorry for late reply.

                                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