Capitalisation - How to Change Just the First Letter of a String/Sentence/Paragraph to Capitals

I have a variable containing some text where I just want to change the first letter to be a capital letter, and leave all the other letters as they are.

I’m struggling to see if there is a function that does that in Wappler or if I need to do something more creative myself.

Any thoughts would be much appreciated as the examples shown in the user interface don’t seem to work…

Best wishes,
Antony.

Doesn’t capitalize() do what you want?

Applies capitalisation to all first letters of the word Tom. Think Antony only requires the first letter of the sentence to be in capitals?

Hmm can’t quote your reply Tom (bug in the forum!)… :slight_smile:

.humanize() seems to do what you’re looking for.

Depending on what your text format looks like, it may not be quite the solution you’re looking for.

Thanks for your input folks!

Unfortunately neither of those work.

I want to change, for example:

on 1st April 2021

to

On 1st April 2021

However they both change it to:

On 1st april 2021

Try the titlecase formatter, it will make each starting letter from each word uppercase. And if that is not the one you are looking for you could create a custom formatter.

I would have thought you had achieved what you described in the title thread - but I see what you want.

Hopefully @patrick has come up with the best and simplest solution. I was about to suggest a less simple solution, but it might be useful anyway. I've used something like this to do multiple things to the words in a string - using multiple .map()s

var1.value.split(' ').map($value.capitalize()).join(' ')"

@TomD not sure why these manipulations, but what Patrick suggested is what Antony needs (shows in his example).

{{('on 1st April 2021').titlecase()}}

returns

On 1st April 2021

Indeed, as I said ("@patrick has come up with the best and simplest solution").

However, it's often useful to know of different solutions to a problem. The solution I suggested did essentially what @Antony asked for - applying formatting to each word in a string. It also offers possibilities than can't be achieved using the built-in formatters. It's a technique I thought @Antony might find useful. It's a technique I learnt from @patrick.

Hey folks, thanks sooo much for all your help!

But...

None of the responses give me the solution... I want to change the case of ONLY the first letter. Nothing else.

So my example maybe wasn't quite as complete. I need

on the evening of 1st April 2021

to become

On the evening of 1st April 2021

I sense I will need to create something a bit more clever to do it! :slight_smile:

So the actual request is - change only the first letter of a string to capitals, and leave the rest unchanged.
In this case you will need something like:

{{var1.value.substr(0,1).capitalize() + var1.value.substr(1)}}

where var1.value is your dynamic value.

Additionally, if it’s just for the presentation and you don’t care how it’s stored you can use CSS. This will save you from having to do the transform all over your app if you need it more than once.

i.e.

p.capitalize {
  text-transform: capitalize;
}

Edit: Sorry I was too lazy to read all the thread. You just want the first letter of the string. You can use the pseudo-element :first-letter

p:first-letter {
  text-transform: uppercase;
}
1 Like

Hey @teodor and @JonL, thank you sooo much for your replies. :slight_smile:

Yes Teodor, what I specifically require is actually the title of the post!

It looks like either of those will work, and since in the actual situation the variable is a more complex {{server_action.data.query.field_name}} I think I’ll go for the CSS.

Brilliant solution Jonas!

1 Like

I fixed the title for you. You didn't mention in it the word "string/sentence/paragraph/group of words". It was an important detail :slight_smile:
That's why my first CSS solution was in line with the others. I had to read the entire thread to understand you were referring to a string.

Oh okay Jonas… it is easy to assume everyone will know I mean of the whole string, which clearly wasn’t the case.

It’s funny, I kept reading the title before you edited it and saying to myself “isn’t this clear to everyone”?

So thanks for that clarification!

1 Like

Actually I changed it again, because I assumed everyone will know what a string is. Which in a nocode context is not true :slight_smile: