For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2005 > Removing a null from within a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Removing a null from within a string
6tc1@qlink.queensu.ca

2005-04-21, 4:00 pm

Without getting into the non-essential details of the application I've
written - at a high level I'm connecting to an LDAP directory, getting
data and then sending it over a different protocol to a different
application.

Anyway, in the process of getting the data (using JNDI) from the
directory I end up with a string with a null in it. It actually took
me about an hour to determine that this was in fact the problem. Once
I had determined that I tried many different techniques to get rid of
the null (specifically to only get the text up until the null) - one of
which was the following:
String nullString = null;
int posOfNull = text.indexOf(nullString);
if (posOfNull != -1){
text = text.substring(0,posOfNull);
}

The above seemed to work on strings that didn't contain nulls, but
seemed to fail on the string that had it.

This was the solution I finally used:
char [] charArray = text.toCharArray();
int posOfNull = -1;
for (int j =0;(j< charArray.length)&&(posOfNull < 0);j++){
if (charArray[j] == 0x0){
posOfNull = j;
}
}
if (posOfNull != -1){
text = new String(charArray,0,posOfNull);
}

Is the above really the best way to get rid of nulls from within a
String - it seems terribly inefficient. Though the algorithm is O(n) -
AKA linear - it isn't making use of any of the native* String handling
methods.

Thanks,
Novice

*By native I mean those Java methods that are written in C/C++ or some
other low level language and are thus more efficient.

Eric Sosman

2005-04-21, 4:00 pm



6tc1@qlink.queensu.ca wrote:
> Without getting into the non-essential details of the application I've
> written - at a high level I'm connecting to an LDAP directory, getting
> data and then sending it over a different protocol to a different
> application.
>
> Anyway, in the process of getting the data (using JNDI) from the
> directory I end up with a string with a null in it.


By "string with a null in it" I guess you mean "string
containing a zero-valued `char'." It might be a good idea to
use some word other than "null" to describe this condition,
because `null' is a Java keyword and anyone reading the word
`null' in a Java context is going to assume you mean "The
reference value that refers to no Object." This is bound
to create confusion ...

> It actually took
> me about an hour to determine that this was in fact the problem. Once
> I had determined that I tried many different techniques to get rid of
> the null (specifically to only get the text up until the null) - one of
> which was the following:
> String nullString = null;
> int posOfNull = text.indexOf(nullString);


... and here's the proof: You even managed to confuse
yourself! The "String reference that refers to no String"
has nothing at all to do with a String containing a zero
character.

> if (posOfNull != -1){
> text = text.substring(0,posOfNull);
> }
>
> The above seemed to work on strings that didn't contain nulls, but
> seemed to fail on the string that had it.


I'm a little surprised that the indexOf(String) method
didn't throw a NullPointerException. The Javadoc makes no
mention of what happens with a `null' argument, and that
usually means `null' arguments are not expected and will
cause trouble. But I guess indexOf(String) makes a special
case for this. (I wonder what value it returns: an argument
could be made for either -1 or for 0. Hmmm: I tried it, and
I got NullPointerException -- is this actually the code that
"seemed to work," or is it a paraphrase?)

> This was the solution I finally used:
> char [] charArray = text.toCharArray();
> int posOfNull = -1;
> for (int j =0;(j< charArray.length)&&(posOfNull < 0);j++){
> if (charArray[j] == 0x0){
> posOfNull = j;
> }
> }
> if (posOfNull != -1){
> text = new String(charArray,0,posOfNull);
> }
>
> Is the above really the best way to get rid of nulls from within a
> String - it seems terribly inefficient. Though the algorithm is O(n) -
> AKA linear - it isn't making use of any of the native* String handling
> methods.


int posOfNull = text.indexOf('\u0000');
if (posOfNull >= 0)
text = text.substring(0, posOfNull);

Like your code, this discards any characters that may follow
the first zero.

--
Eric.Sosman@sun.com

Patricia Shanahan

2005-04-21, 4:00 pm

6tc1@qlink.queensu.ca wrote:

> *By native I mean those Java methods that are written in
> C/C++ or some other low level language and are thus more
> efficient.
>


There are very few of those, mainly ones that were necessary
for functional reasons. String and StringBuffer are written
in java, as is java.util.regex, so you are very unlikely to
find a "native" method, by that standard, that is any help
with string searching and edition. In particular, indexOf
and substring are both written in Java.

Patricia


6tc1@qlink.queensu.ca

2005-04-21, 4:00 pm

Sorry for mixing up terminology... you are right null and the "zero
character" are not the same... though I would argue that "zero
character" could as easily be with the number zero as a
character - i.e.:
'0'

I think you are right about the first algorithm I posted - i.e. that it
did throw a NullPointerException the first time it was used in that
way.

Thanks for the code - it seems to work fine.

Novice

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com