Friday, December 26, 2025

Java Trim

 

When String.trim() “Fails” in Java (And Why It’s Not a Bug)

Every Java developer eventually runs into this head-scratcher:

String s = " hello "; System.out.println(s.trim());

And yet… the output still looks padded.

At first glance, it feels like trim() is broken.

It isn’t.

What’s broken is the assumption about what trim() actually removes.

Let’s clear this up once and for all.


What String.trim() Really Does

In Java, String.trim() removes leading and trailing characters whose Unicode code point is ≤ U+0020.

That’s it.

This includes:

  • Regular space (U+0020)

  • Tabs (\t)

  • Newlines (\n)

  • Carriage returns (\r)

  • A handful of other low-level control characters

If the character’s Unicode value is greater than 0x20, trim() will leave it alone, even if it looks like whitespace.


The #1 Reason trim() Appears to Fail

Non-breaking spaces (U+00A0)

This is the usual culprit.

Non-breaking spaces:

  • Look exactly like normal spaces

  • Commonly appear when copying text from:

    • Web pages

    • PDFs

    • Excel

    • Word documents

  • Are not removed by trim()

Example:

String s = "\u00A0hello\u00A0"; System.out.println(s.trim()); // still padded

This isn’t a bug. Java is doing exactly what it’s documented to do.


Other Invisible Characters That trim() Won’t Touch

Some especially nasty ones:

  • \u00A0 – non-breaking space

  • \u200B – zero-width space

  • \u2007 – figure space

  • \u202F – narrow no-break space

These characters:

  • Render invisibly

  • Survive trim()

  • Can break comparisons, parsing, validation, and logging


Common Misunderstandings

❌ “trim removes all whitespace”

Nope. Only specific characters.

❌ “trim cleans the whole string”

Nope. Only leading and trailing characters. Internal whitespace is untouched.

❌ “trim failed, so Java is buggy”

Also nope. This behavior is explicitly defined in the JDK.


How to Prove What’s Actually in Your String

When a string refuses to trim, inspect the Unicode values:

for (int i = 0; i < s.length(); i++) { System.out.printf( "char[%d] = '%c' (U+%04X)%n", i, s.charAt(i), (int) s.charAt(i) ); }

Once you see U+00A0 or U+200B, the mystery is over.


The Right Way to Trim All Whitespace

If your input comes from:

  • Files

  • Excel

  • Web APIs

  • Copy-paste

  • User input

…then trim() is usually not enough.

Unicode-aware trim using regex

s = s.replaceAll("^\\s+|\\s+$", "");

This handles:

  • Standard whitespace

  • Unicode whitespace

  • Much closer to what people expect “trim” to mean

Aggressive cleanup (separators + control chars)

s = s.replaceAll("^[\\p{Z}\\p{C}]+|[\\p{Z}\\p{C}]+$", "");

Where:

  • \p{Z} = Unicode separators (spaces of all kinds)

  • \p{C} = control and invisible characters

Use this when dealing with truly messy input.


One Last Gotcha: null

Don’t forget:

s.trim(); // throws NullPointerException if s == null

If there’s any chance of nulls, guard it or normalize earlier.


Takeaway

String.trim() doesn’t fail.

It just:

  • Works exactly as specified

  • Assumes you know Unicode

  • Is often insufficient for real-world data

If your strings come from outside your JVM, assume they’re dirty—and clean them properly.

If you’ve ever lost time debugging a “trim bug”, now you know: it wasn’t Java.

US Holidays for 2025 and beyond

 I wrote some Excel VBA code to create a list of US Federal holidays.

My GitHub link: https://github.com/64board/ExcelVBA/blob/master/USHolidays.bas

 

Friday, May 31, 2024

How to Kill Zombie Processes on Linux

We can list zombie processes by using the ps command and piping its output into egrep command. Zombie processes have a state flag of "Z," and you'll usually also see "defunct."

 Type the following command on a terminal:

ps aux | egrep "Z|defunct"

The zombie process will be listed and you can also see the program that spawned these zombies.

Once we have the process ID of the first zombie, we need to find the process ID of its parent process. We can do so by using ps again. We'll use the output option (-o) to tell ps to display only the parent's process ID, and then pass it with the ppid= flag.

The process we want to find will be indicated by using the -p (process) option, and then passing in the zombie's process ID.

Therefore, we type the following command to look up the process information for process with ID = 7641, and it will only report the ID of the parent process:

ps -o ppid= -p 7641

The output of the command will be the ID of the parent process then you can use the kill command to terminate the parent process if that is safe to do and the zombie process will go away.

Taken from: https://www.howtogeek.com/701971/how-to-kill-zombie-processes-on-linux/

Tuesday, May 28, 2024

Calculating time diff between two date times in Perl

A short script for calculating difference between two date times in Perl.

See source code on GitHub: https://github.com/64board/Perl/blob/main/time_diff_time_piece.pl

The Perl Time::Piece module: https://perldoc.perl.org/Time::Piece

The excellent Calendar FAQ at http://www.tondering.dk/claus/calendar.html


Wednesday, February 7, 2024

Holidays API from https://dev.timeanddate.com/holidays/

 Sample code here: https://gist.github.com/64board/0f9485c4140f62565e02ca941417f68f

Output:

2024-01-01 New Year's Day
2024-01-15 Martin Luther King Jr. Day
2024-02-19 Presidents' Day
2024-05-27 Memorial Day
2024-06-19 Juneteenth
2024-07-04 Independence Day
2024-09-02 Labor Day
2024-10-14 Columbus Day
2024-11-11 Veterans Day
2024-11-28 Thanksgiving Day
2024-12-25 Christmas Day

 

 

Thursday, January 25, 2024

2024 Holiday Schedule

 

Date Holiday
Monday, January 01 New Year’s Day
Monday, January 15 Birthday of Martin Luther King, Jr.
Monday, February 19 * Washington’s Birthday
Monday, May 27 Memorial Day
Wednesday, June 19 Juneteenth National Independence Day
Thursday, July 04 Independence Day
Monday, September 02 Labor Day
Monday, October 14 Columbus Day
Monday, November 11 Veterans Day
Thursday, November 28 Thanksgiving Day
Wednesday, December 25 Christmas Day

*This holiday is designated as "Washington’s Birthday" in section 6103(a) of title 5 of the United States Code, which is the law that specifies holidays for Federal employees. Though other institutions such as state and local governments and private businesses may use other names, it is our policy to always refer to holidays by the names designated in the law.

Wednesday, December 13, 2023

Federal Holidays 2023

 

Date Holiday
Monday, January 02 * New Year’s Day
Monday, January 16 Birthday of Martin Luther King, Jr.
Monday, February 20 ** Washington’s Birthday
Monday, May 29 Memorial Day
Monday, June 19 Juneteenth National Independence Day
Tuesday, July 04 Independence Day
Monday, September 04 Labor Day
Monday, October 09 Columbus Day
Friday, November 10 * Veterans Day
Thursday, November 23 Thanksgiving Day
Monday, December 25 Christmas Day

*If a holiday falls on a Saturday, for most Federal employees, the preceding Friday will be treated as a holiday for pay and leave purposes. (See 5 U.S.C. 6103(b).) If a holiday falls on a Sunday, for most Federal employees, the following Monday will be treated as a holiday for pay and leave purposes. (See Section 3(a) of Executive Order 11582, February 11, 1971.) See also our Federal Holidays – "In Lieu Of" Determination Fact Sheet at https://www.opm.gov/policy-data-oversight/pay-leave/work-schedules/fact-sheets/Federal-Holidays-In-Lieu-Of-Determination.

**This holiday is designated as "Washington’s Birthday" in section 6103(a) of title 5 of the United States Code, which is the law that specifies holidays for Federal employees. Though other institutions such as state and local governments and private businesses may use other names, it is our policy to always refer to holidays by the names designated in the law.

Taken from: https://www.opm.gov/policy-data-oversight/pay-leave/federal-holidays/#url=2023