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. Removing null characters from strings

Removing null characters from strings

Scheduled Pinned Locked Moved C#
question
6 Posts 4 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.
  • S Offline
    S Offline
    S O S
    wrote on last edited by
    #1

    If I have a string that has null characters at the end, how can I remove them? Doing Trim('\0') didn't work and I can't think of anything else to try, besides manually finding the position of the last non-null character and then removing everything after it or something like that.

    N B 2 Replies Last reply
    0
    • S S O S

      If I have a string that has null characters at the end, how can I remove them? Doing Trim('\0') didn't work and I can't think of anything else to try, besides manually finding the position of the last non-null character and then removing everything after it or something like that.

      N Offline
      N Offline
      NHM
      wrote on last edited by
      #2

      r u using CString? Nuno Henrique Mendes

      1 Reply Last reply
      0
      • S S O S

        If I have a string that has null characters at the end, how can I remove them? Doing Trim('\0') didn't work and I can't think of anything else to try, besides manually finding the position of the last non-null character and then removing everything after it or something like that.

        B Offline
        B Offline
        Bo Hunter
        wrote on last edited by
        #3

        Trim trims spaces not anything else. If you want to trim other chars you need to use the Trim( new char[]{' ', '\0'} ); or what ever you need. TrimEnd( new char[]{' ', '\0'} ); TrimStart( new char[]{' ', '\0'} );

        S 1 Reply Last reply
        0
        • B Bo Hunter

          Trim trims spaces not anything else. If you want to trim other chars you need to use the Trim( new char[]{' ', '\0'} ); or what ever you need. TrimEnd( new char[]{' ', '\0'} ); TrimStart( new char[]{' ', '\0'} );

          S Offline
          S Offline
          S O S
          wrote on last edited by
          #4

          Yees, but as I thought I said, it didn't work.

          A 1 Reply Last reply
          0
          • S S O S

            Yees, but as I thought I said, it didn't work.

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

            string str; byte [] charStr = new byte[] {(byte)'T', (byte)'e', (byte)'s', (byte)'t', 0, 0, 0, 0, 0, 0}; str = Encoding.ASCII.GetString(charStr); // str now contains "Test\0\0\0\0\0\0"; str = str.TrimEnd(new char[]{'\0'}); // str now contains "Test" This code works. Make sure you are assigning the return value of TrimEnd method to your string. It does not actually trim the contents of string object but rather generates a new trimmed string.

            A 1 Reply Last reply
            0
            • A Acidis

              string str; byte [] charStr = new byte[] {(byte)'T', (byte)'e', (byte)'s', (byte)'t', 0, 0, 0, 0, 0, 0}; str = Encoding.ASCII.GetString(charStr); // str now contains "Test\0\0\0\0\0\0"; str = str.TrimEnd(new char[]{'\0'}); // str now contains "Test" This code works. Make sure you are assigning the return value of TrimEnd method to your string. It does not actually trim the contents of string object but rather generates a new trimmed string.

              A Offline
              A Offline
              Acidis
              wrote on last edited by
              #6

              After looking at docs, this is even better: str = str.TrimEnd('\0'); Also, if you are reading a null terminated string, there is a chance you have garbage characters after the terminating null. This will prevent Trim methods from working. So, your best approach would be: int Index = str.IndexOf('\0'); if (Index >= 0) str = str.Substring(0, Index);

              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