Determining the case (upper/lower) of the first letter in a string

蹲街弑〆低调 提交于 2019-12-10 10:16:46

问题


In a web application, how do I determine whether the first letter in a given string is upper- or lower-case using JavaScript?


回答1:


You can use toUpperCase:

if(yourString.charAt(0) === yourString.charAt(0).toUpperCase()) {
    //Uppercase!
}

If you're going to be using this on a regular basis, I would suggest putting it in a function on the String prototype, something like this:

String.prototype.isFirstCapital = function() {
    return this.charAt(0) === this.charAt(0).toUpperCase();   
}
if(yourString.isFirstCapital()) {
    //Uppercase!
}

Update (based on comments)

I don't know what you actually want to do in the case that the string does not being with a letter, but a simple solution would be to add a quick check to see if it does or not, and return false if not:

String.prototype.isFirstCapital = function() {
    return /^[a-z]/i.test(this) && this.charAt(0) === this.charAt(0).toUpperCase();   
}



回答2:


This will work only with English alphabet.

var ch = myStr.chatAt(0);
if (ch >= 'a' && ch <= 'z') {
    // small
} else if (ch >= 'A' && ch <= 'Z') {
    // capital
} else {
    // not english alphabet char
}



回答3:


var mystring = "Test string";
var first= "";
if (mystring )
{
    first= mystring[1];
}


if (first)
{
    $('p').each(function()
    {
        if ($(this).text().charAt(0).toUpperCase() === $(this).text().charAt(0))
        {
           alert("Uppercase");
        }
    });
}



回答4:


This will be called recursively until a first letter in a string is approached, otherwise returns 'no letters'.

function getFirstCase(string) {
    if (string === '') return 'no letters';

    var firstChar = string.charAt(0);

    /*
     * If both lowercase and uppercase
     * are equal, it is not a letter
     */
    if (firstChar.toLowerCase() === firstChar.toUpperCase()) {
        return getFirstCase(string.substr(1));
    } else {
        return firstChar.toLowerCase() === firstChar ? 'lowercase' : 'uppercase';
    }
}

Testing:

console.log(getFirstCase('alphabet'), 
            getFirstCase('Sunshine'),
            getFirstCase('123123'),
            getFirstCase('@Hi'),
            getFirstCase('\nHAHA'));



回答5:


I'm surprised no one's offered a regex solution to this - it seems like the easiest by far:

function getFirstCase(s) {
    return (/^[\d\W]*[A-Z]/).test(s) ? 'upper' :
        (/^[\d\W]*[a-z]/).test(s) ? 'lower' :
        'none';
}

Blatantly stealing @Lapple's test cases:

console.log(getFirstCase('alphabet'), 
            getFirstCase('Sunshine'),
            getFirstCase('123123'),
            getFirstCase('@Hi'),
            getFirstCase('\nHAHA'));

// lower upper none upper upper

See http://jsfiddle.net/nrabinowitz/a5cQa/



来源:https://stackoverflow.com/questions/7824317/determining-the-case-upper-lower-of-the-first-letter-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!