7 Replies Latest reply: Dec 5, 2010 4:03 PM by jschellSomeoneStoleMyAlias RSS

    How to erase null characters from string?

    821184
      Hello
      I cast an array to a string with String(array). The array (type byte) contains ascii byte codes, but there are always some empty spots in the end of the array. When casted to a string, these emptys will show up as squares (null's?) when the string is printed. I want to split the string so I can get a clean string with only the first meaningfull characters. Can anyone help me on this? I have tried
      replaceAll(null, ""); with no luck (the whole string is erased).

      greetings from Henrik

      Edited by: user13418034 on Dec 4, 2010 1:24 PM
        • 1. Re: How to erase null characters from string?
          796440
          user13418034 wrote:
          Hello
          I cast an array to a string
          No you didn't. Casting an array to a String would be
          Something[] a = ...;
          String s = (String)a;
          and that won't compile.

          with String(array).
          Excuse me?

          Provide an [url http://sscce.org]SSCCE that shows exactly what you did.

          Edited by: jverd on Dec 4, 2010 2:11 PM
          • 2. Re: How to erase null characters from string?
            EJP
            new String(array, offset, length). This is not casting, it is conversion, or construction.
            • 3. Re: How to erase null characters from string?
              YoungWinston
              user13418034 wrote:
              I cast an array to a string with String(array). The array (type byte) contains ascii byte codes, but there are always some empty spots in the end of the array. When casted to a string, these emptys will show up as squares (null's?) when the string is printed. I want to split the string so I can get a clean string with only the first meaningfull characters. Can anyone help me on this? I have tried
              replaceAll(null, ""); with no luck (the whole string is erased).
              That's because:
              (a) a byte (or indeed a char, which is probably what it will be converted to) cannot contain 'null'.
              (b) replaceAll() takes Strings as arguments; what you want is to replace characters. You could try
              replace((char)0, ' ');
              but it probably isn't exactly what you want.

              My suggestion would be to write a
              public int trimmedLength(byte[] array) {...
              method that returns the length of your byte array minus its trailing nulls, and use it with [url http://download.oracle.com/javase/6/docs/api/java/lang/String.html#String%28byte[],%20int,%20int%29]this constructor, as EJP suggested.

              Winston
              • 4. Re: How to erase null characters from string?
                DarrylBurke
                new String(bytes).replace("\u0000", "")
                where bytes is a byte array.

                db
                • 5. Re: How to erase null characters from string?
                  YoungWinston
                  Darryl Burke wrote:
                  new String(bytes).replace("\u0000", "")
                  where bytes is a byte array.
                  Yup. Keep forgetting these new-fangled 1.5 methods :-) .

                  Winston
                  • 6. Re: How to erase null characters from string?
                    DarrylBurke
                    YoungWinston wrote:
                    Yup. Keep forgetting these new-fangled 1.5 methods :-) .
                    You can use this in 1.4
                    new String(bytes).replaceAll("\u0000", "")
                    db
                    • 7. Re: How to erase null characters from string?
                      jschellSomeoneStoleMyAlias
                      user13418034 wrote:
                      Hello
                      I cast an array to a string with String(array). The array (type byte) contains ascii byte codes, but there are always some empty spots in the end of the array. When casted to a string, these emptys will show up as squares (null's?) when the string is printed. I want to split the string so I can get a clean string with only the first meaningfull characters. Can anyone help me on this? I have tried
                      replaceAll(null, ""); with no luck (the whole string is erased).
                      Why don't you get the array without nulls in the first place?