Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Removing a null from within a string
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.


Report this thread to moderator Post Follow-up to this message
Old Post
6tc1@qlink.queensu.ca
04-21-05 09:00 PM


Re: Removing a null from within a string

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


Report this thread to moderator Post Follow-up to this message
Old Post
Eric Sosman
04-21-05 09:00 PM


Re: Removing a null from within a string
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



Report this thread to moderator Post Follow-up to this message
Old Post
Patricia Shanahan
04-21-05 09:00 PM


Re: Removing a null from within a string
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


Report this thread to moderator Post Follow-up to this message
Old Post
6tc1@qlink.queensu.ca
04-21-05 09:00 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.