Archive for August, 2018

Python Logo Unity Logo

Platform: Any/Unity
Language: Python/C#
Requirements: None

Many of you may be familiar with the term Title Case in conjunction with text handling. It is typically used for things, such as book titles, game titles, movie titles, and so forth.
The common perception appears to be that writing a name or headline in title case means to simply capitalize the first letter of each word, like this.

Snow White And The Seven Dwarfs

This, however, is not really how title cases work. The correct application of title cases asks for the capitalization of the first letter of each word, except for certain small words, such as articles and short prepositions. Therefore, the correct way to write the example above would be…

Snow White and the Seven Dwarfs

Unfortunately, most programming languages and libraries get this completely wrong and, by default, offer title case implementations that are simply not correct. To make up for that deficiency, I decided to write my own implementation that I am making available to you here in Python and C#, for use in Unity.

The approach is simple. I create a list containing articles, conjunctions, and prepositions that I am using to identify words in a string that should not be capitalized. Going through the string, word by word, I then create a new output string with the respective words starting with a capital or lowercase letter. Couldn’t be simpler, really, and the advantage of the list is that it can be easily adapted to include certain words for you individual special cases.

Simple steps to put it to work

Here’s a small test-suite to see the implementation at work…

As you will see, it correctly generates the following output, just the way it should be.


How to Interpret "The Hitchhiker's Guide to the Galaxy"
Snow White and the Seven Dwarfs
Newcastle upon Tyne
Brighton on Sea
A Dog's Tale
The Last of the Mohicans
How to Be Smart
About a Boy
Reading 'Fight Club' Through a Postmodernist Lens
How to Interpret “The Hitchhiker's Guide to the Galaxy”
Reading ‘Fight Club’ Through a Postmodernist Lens

Let’s do title cases in Unity

I’ve also implemented the same thing in C#, as an extension to the string class.

Implementing it as an extension of the string class makes the usage of the function a lot easier and more seamless. It allows you to call directly on any string via the string.ProperTitleCase() method.

Here is a small code snippet that shows you how you would use this in a script in Unity.

There you go! Let’s make half-baked title cases a thing of the past.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Commatizing numbers in Python


Platform: Any
Language: Python
Requirements: None

Earlier today, I stumbled across a small programming challenge on rosettacode.org that required a solution in Python. I was intrigued enough to give it a shot and below you will find my approach to “commatizing” numbers in string.

The challenge is simple. Take a string, see if it contains numbers and then format the numbers in a specific way, by clustering the digits and inserting separators like commas, periods, blanks, or blanks, to make them more readable.

In essence, the string
pi=3.14159265358979323846264338327950288419716939937510582097494459231
will be turned into something like
pi=3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59231

The approach needs to be flexible so that clusters can have a variable length and the separator can be defined.

The workings of the code are very simple. I use a regular expression to locate series of digits in the string and then iterate through the resulting match to create a new output string. I am skipping the last match because it represents the End-of-string marker.

Whenever RegEx encounters a non-digit, it will create an empty match, so as I iterate through matches, I check if a match is empty and simply copy the respective letter from the original string.

If a valid digit match is found, I check the length of the series. If it’s shorter than the desired cluster length, it is copied verbatim, while longer series will be turned into a list of clusters of the desired length. They are then joined back together, using the separator. I am doing this with an often-overlooked little Python trick by applying the join() function to the separator string, while providing the list of clusters as a parameter.

The print statements are simple output code to illustrate the usage and the results of the function.

I hope this is something you can use for yourself some time.

Hang loose!

Facebooktwittergoogle_plusredditpinterestlinkedinmail