Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 697621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:11:13+00:00 2026-05-14T03:11:13+00:00

Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII

  • 0

Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII characters. Taking a dip into the source I found this tidbit:

// remove any periods from first name e.g. Mr. John --> Mr John
firstName = firstName.trim().replace('.','\0');

Does replacing a character in a String with a null character even work in Java? I know that '\0' will terminate a C-string. Would this be the culprit to the funky characters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:11:13+00:00Added an answer on May 14, 2026 at 3:11 am

    Does replacing a character in a String with a null character even work in Java? I know that ‘\0’ will terminate a c-string.

    That depends on how you define what is working. Does it replace all occurrences of the target character with '\0'? Absolutely!

    String s = "food".replace('o', '\0');
    System.out.println(s.indexOf('\0')); // "1"
    System.out.println(s.indexOf('d')); // "3"
    System.out.println(s.length()); // "4"
    System.out.println(s.hashCode() == 'f'*31*31*31 + 'd'); // "true"
    

    Everything seems to work fine to me! indexOf can find it, it counts as part of the length, and its value for hash code calculation is 0; everything is as specified by the JLS/API.

    It DOESN’T work if you expect replacing a character with the null character would somehow remove that character from the string. Of course it doesn’t work like that. A null character is still a character!

    String s = Character.toString('\0');
    System.out.println(s.length()); // "1"
    assert s.charAt(0) == 0;
    

    It also DOESN’T work if you expect the null character to terminate a string. It’s evident from the snippets above, but it’s also clearly specified in JLS (10.9. An Array of Characters is Not a String):

    In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by ‘\u0000’ (the NUL character).


    Would this be the culprit to the funky characters?

    Now we’re talking about an entirely different thing, i.e. how the string is rendered on screen. Truth is, even “Hello world!” will look funky if you use dingbats font. A unicode string may look funky in one locale but not the other. Even a properly rendered unicode string containing, say, Chinese characters, may still look funky to someone from, say, Greenland.

    That said, the null character probably will look funky regardless; usually it’s not a character that you want to display. That said, since null character is not the string terminator, Java is more than capable of handling it one way or another.


    Now to address what we assume is the intended effect, i.e. remove all period from a string, the simplest solution is to use the replace(CharSequence, CharSequence) overload.

    System.out.println("A.E.I.O.U".replace(".", "")); // AEIOU
    

    The replaceAll solution is mentioned here too, but that works with regular expression, which is why you need to escape the dot meta character, and is likely to be slower.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please check out this code, i really dont understand why i got
At work, we do distributed testing, where rsync out to everyone elses machine and
I've been testing out the performance and memory profiler AQTime to see if it's
I'm testing out my application with the hopes of migrating to SQL Server 2008
I'm finally starting out with unit testing, having known that I should be doing
I am starting out with unit testing, I have a method that uses the
I am starting out with automated testing and I would like to test one
I'm having trouble figuring out how to get the testing framework set up and
I am just starting out with DI & unit testing and have hit a
I personally don't use FxCop yet. We want to work out the unit testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.