Convert CamelCase to Title Case


#1

Hello,

I’m having trouble converting camel case to title case.

I have a ton of strings named in camel case, for example “ProjectOne”, “ProjectTwo”.
I’d like to make these into title case, for example “Project One”, “Project Two”.

I haven’t been able to find a way to do this and I haven’t found a package to do it.

Thanks in advance!


#2

You can use node “String.Replace” and replace all uppercase character to " " (eg “A” to " A") and then use “TrimLeadingWhitespace”.


#3

I haven’t gotten that to work, especially when I input a list of strings.
Am I missing a step?


#4

Unfortunately you must every character replace individually.
First replace “A” to " A", next “B” to " B" etc.


#5

Ah I see. Is there any other way to do this? Maybe some If statement that will check if a character is upper?
I was trying to do that before but it turned into a really long script, so I was wondering if there’s an optimal way of doing it.


#6

For example, here’s me second test but I’ve gotten strange results and haven’t gotten that to work.


#7


#8

Looks like @Robert2017 has got you a node solution! Here is a Python solution as well:
camelcasetotitlecase

import re

camels = IN[0]
titles = []
def convert(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1 \2', s1)

for i in camels:
	titles.append(convert(i))

OUT = titles

#9

I definitely didn’t realize it would be so complicated. Thanks for figuring that out!


#10

This looks great as well!
I got way too confused trying to do it in Python but this seems to do the trick.
I’ll probably wind up using your solution since it seems simpler. Thanks so much!


#11

Hey! I finally got what I was trying to do in Python to work as well. Just for the fun of it and my own sanity. I put + instead of a += so that’s why my code wasn’t working. Here’s what I got to work as well. I’ll leave it here in case you’re curious.
All of these work though! Thanks for the help!

old = IN[0]
OUT = ''

for char in old:
  if char.isupper():
    OUT += ' ' + char
  else:
    OUT += char

OUT = OUT.strip()

print(OUT)

Yours is better anyway because it works with lists. :slight_smile: