Counting upper and lower case characters in a string

后端 未结 7 578
礼貌的吻别
礼貌的吻别 2020-12-19 03:25

First off, I know this is far from professional. I\'m trying to learn how to work with strings. What this app is supposed to do is take a simple text input and do a few thi

7条回答
  •  一生所求
    2020-12-19 04:16

    Most of the solutions here will fail when string contains UTF8 or diacritic characters. An improved version that works with all strings can be found at the turbocommons library, here:

    https://github.com/edertone/TurboCommons/blob/1e230446593b13a272b1d6a2903741598bb11bf2/TurboCommons-Php/src/main/php/utils/StringUtils.php#L391

    Example:

    // Returns 2
    StringUtils.countByCase('1声A字43B45-_*[]', StringUtils.FORMAT_ALL_UPPER_CASE);
    
    // Returns 1
    StringUtils.countByCase('1声A字43B45a-_*[]', StringUtils.FORMAT_ALL_LOWER_CASE);
    

    More info here:

    https://turbocommons.org/en/blog/2019-10-15/count-capital-letters-in-string-javascript-typescript-php

    Play with it online here:

    https://turbocommons.org/en/app/stringutils/count-capital-letters

提交回复
热议问题