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. How to remove a character from a string - help required

How to remove a character from a string - help required

Scheduled Pinned Locked Moved C#
questionhelpcsharptutorial
7 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.
  • J Offline
    J Offline
    James19841984
    wrote on last edited by
    #1

    Hi, I have a C# project which imports a .csv file, however some of the characters in the file are square characters. These characters mean that when I write the output to a text file I get a newline where the square characters were. My question is how can I remove these square characters? If I wanted to remove say the character value "s" I could write: String holder = "susan"; char[] myChar = {'s'}; String newString = holder.Trim(myChar); Output = "uan" The problem is I don't know how to remove the square character because it is not a conventional ASCII character. Any help is much appreciated. Thanks James

    N V C 3 Replies Last reply
    0
    • J James19841984

      Hi, I have a C# project which imports a .csv file, however some of the characters in the file are square characters. These characters mean that when I write the output to a text file I get a newline where the square characters were. My question is how can I remove these square characters? If I wanted to remove say the character value "s" I could write: String holder = "susan"; char[] myChar = {'s'}; String newString = holder.Trim(myChar); Output = "uan" The problem is I don't know how to remove the square character because it is not a conventional ASCII character. Any help is much appreciated. Thanks James

      V Offline
      V Offline
      Vitaliy Tsvayer
      wrote on last edited by
      #2

      Not clear for me what you actually try to do, but hope short info will lead you to the solution. Unix environments use "\n" for newline, while Windows uses combination "\r\n". While in C you can use "\n" to indicate newline for both Unix & Windows environments, in c# you should specify explicitly what you want that is "\r\n" for Windows, and even better you should use Environment.NewLine SO, my guess is your CVS file is from Unix like environment so you should replace single "\n"s with "\r\n" OR you should correct the code which generates .CVS file to use correct new line value.

      Vitaliy Tsvayer Tikle

      1 Reply Last reply
      0
      • J James19841984

        Hi, I have a C# project which imports a .csv file, however some of the characters in the file are square characters. These characters mean that when I write the output to a text file I get a newline where the square characters were. My question is how can I remove these square characters? If I wanted to remove say the character value "s" I could write: String holder = "susan"; char[] myChar = {'s'}; String newString = holder.Trim(myChar); Output = "uan" The problem is I don't know how to remove the square character because it is not a conventional ASCII character. Any help is much appreciated. Thanks James

        N Offline
        N Offline
        Nader Elshehabi
        wrote on last edited by
        #3

        Hello How did you display your CSV file to view the square char?? Using the notepad maybe?

        James19841984 wrote:

        The problem is I don't know how to remove the square character because it is not a conventional ASCII character.

        AFAIK it should have a code no matter what!! Try reading a known file char by char until you reach this char's position, then display its code in a messagebox or something. Another thing, open a CSV file with an editor -eg notepad-, copy the square char -if you know that ALL squares are the same char. Sometimes the notepade jst display a square for several chars that it doesn't understand. When you copy the char, paste it into your code -ie. hardcode it- Also use:

        char Sq; //Hardcode it here!!
        While(MyString.IndexOf(Sq) != -1)
        MyString.Remove(MyString.IndexOf(Sq), 1);

        Regards:rose:

        J 1 Reply Last reply
        0
        • N Nader Elshehabi

          Hello How did you display your CSV file to view the square char?? Using the notepad maybe?

          James19841984 wrote:

          The problem is I don't know how to remove the square character because it is not a conventional ASCII character.

          AFAIK it should have a code no matter what!! Try reading a known file char by char until you reach this char's position, then display its code in a messagebox or something. Another thing, open a CSV file with an editor -eg notepad-, copy the square char -if you know that ALL squares are the same char. Sometimes the notepade jst display a square for several chars that it doesn't understand. When you copy the char, paste it into your code -ie. hardcode it- Also use:

          char Sq; //Hardcode it here!!
          While(MyString.IndexOf(Sq) != -1)
          MyString.Remove(MyString.IndexOf(Sq), 1);

          Regards:rose:

          J Offline
          J Offline
          James19841984
          wrote on last edited by
          #4

          Thanks for the replys. I have tried "\n", environment.newline etc but none of them seem to work. The reason for this I believe is because C# doesn't recognise the character as a newline, but simply a character in the shape of a square. When I copy and paste the symbol into a .net environment it just pastes a newline (ie, like it would I key return). The symbol will have a character code, but I can't seem to find out what it is. I believe if I have that I can remove these characters. Any other ideas or knowledge about the character code would be much appreciated. Regards James

          C N 2 Replies Last reply
          0
          • J James19841984

            Thanks for the replys. I have tried "\n", environment.newline etc but none of them seem to work. The reason for this I believe is because C# doesn't recognise the character as a newline, but simply a character in the shape of a square. When I copy and paste the symbol into a .net environment it just pastes a newline (ie, like it would I key return). The symbol will have a character code, but I can't seem to find out what it is. I believe if I have that I can remove these characters. Any other ideas or knowledge about the character code would be much appreciated. Regards James

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            James19841984 wrote:

            The reason for this I believe is because C# doesn't recognise the character as a newline, but simply a character in the shape of a square.

            C# recognises no such thing. It will be the text renerizer that displays the square. Usually a square is rendered in place of a character that has no shape defined for the font used. C# recognises characters based solely on their code.

            James19841984 wrote:

            When I copy and paste the symbol into a .net environment it just pastes a newline (ie, like it would I key return).

            Perhaps the renderize is being forced to render a newline.


            Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

            1 Reply Last reply
            0
            • J James19841984

              Thanks for the replys. I have tried "\n", environment.newline etc but none of them seem to work. The reason for this I believe is because C# doesn't recognise the character as a newline, but simply a character in the shape of a square. When I copy and paste the symbol into a .net environment it just pastes a newline (ie, like it would I key return). The symbol will have a character code, but I can't seem to find out what it is. I believe if I have that I can remove these characters. Any other ideas or knowledge about the character code would be much appreciated. Regards James

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #6

              Hello There is another approach, yet I don't prefer it. Try to exclude any character outside the range of the ascii code of letters, digits, and any other symbol that you allow. If the character is outside that range delete it from your CSV string. This way you'd make sure only the things you want will survive. Another approach. Read the file using HEX editor. Find the value of the byte(s) representing the character that is bugging you -or your program:)-. Anyway. When you find the byte(s) value, read your file as an array of bytes and remove the "square" chars by their byte values. I don't think there are more approaches. If this doesn't work, call NASA:laugh: Regards:rose:

              1 Reply Last reply
              0
              • J James19841984

                Hi, I have a C# project which imports a .csv file, however some of the characters in the file are square characters. These characters mean that when I write the output to a text file I get a newline where the square characters were. My question is how can I remove these square characters? If I wanted to remove say the character value "s" I could write: String holder = "susan"; char[] myChar = {'s'}; String newString = holder.Trim(myChar); Output = "uan" The problem is I don't know how to remove the square character because it is not a conventional ASCII character. Any help is much appreciated. Thanks James

                C Offline
                C Offline
                Coding C
                wrote on last edited by
                #7

                Hi, i also faced same problem some days back. but i found the ans. you can use '\r\n' for that newline character. here is the example string strTemp="Store Your String Here!"; strTemp.Replace("\r\n",""); hope this works. Nitin...

                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