Count capital letters in string on Javascript, Typescript and Php

It is not common that we need to count capital letters in our daily development tasks, but sometimes having a method that does exactly this may be a good time saver. The idea is fairly simple: given a raw string which contains some arbitrary text, we want to know how many of its characters are upper case or "Capital letters".

Using the TurboCommons library this process is as simple as calling a method and getting it's result.

You can play and test it online here

Counting capital letters with the TurboCommons library

There's a general purpose countByCase() method inside the StringUtils class which lets you count the number of characters inside a string that match the specified letter case. Let's see how to do it with the specific UPPER case for different programming languages:

Count capital letters in Php

Download the latest TurboCommons phar file from the downloads section, place it on your project as a dependency and run the following code:

require '%path-to-your-project-dependencies-folder%/turbocommons-php-X.X.X.phar';
use org\turbocommons\src\main\php\utils\StringUtils;
echo StringUtils::countByCase('hello')."<br>";
echo StringUtils::countByCase('Hello')."<br>";
echo StringUtils::countByCase('HELlo')."<br>";
echo StringUtils::countByCase('HELLO')."<br>";

The output of the previous code should be:

0
1
3
5

Count capital letters in Javascript on a website

Download the latest turbocommons-es5.js file from the downloads section or use npm to add the dependecy to your project (npm install turbocommons-es5). Then run the following code:

<script src="turbocommons-es5/turbocommons-es5.js"></script>
<script>
var StringUtils = org_turbocommons.StringUtils;
console.log(StringUtils.countByCase('hello'));
console.log(StringUtils.countByCase('Hello'));
console.log(StringUtils.countByCase('HELlo'));
console.log(StringUtils.countByCase('HELLO'));
</script>

The console log for the previous code should be:

0
1
3
5

Count capital letters in Typescript (TS)

The recommended way is to use npm to obtain the turbocommons dependency by executing the following command at the root of your project:

npm install turbocommons-ts

Or you can download the latest turbocommons-ts files from the downloads section and copy the dependency by yourself. Then run the following code:

import { StringUtils } from 'turbocommons-ts';
console.log(StringUtils.countByCase('hello'));
console.log(StringUtils.countByCase('Hello'));
console.log(StringUtils.countByCase('HELlo'));
console.log(StringUtils.countByCase('HELLO'));

The output of the previous code should be:

0
1
3
5

Count capital letters in a NodeJs App

Install the dependency by executing the following command at the root of your project:

npm install turbocommons-ts

And then run the following code:

const {StringUtils} = require('turbocommons-ts');
console.log(StringUtils.countByCase('hello'));
console.log(StringUtils.countByCase('Hello'));
console.log(StringUtils.countByCase('HELlo'));
console.log(StringUtils.countByCase('HELLO'));

Which should output:

0
1
3
5