简体   繁体   English

确定字符串中第一个字母的大小写(上/下)

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

在Web应用程序中,如何使用JavaScript确定给定字符串中的第一个字母是大写还是小写?

You can use toUpperCase : 你可以使用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原型的函数中,如下所示:

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: 在字符串没有字母的情况下,我不知道你真正想做什么,但一个简单的解决方案是添加一个快速检查以确定它是否有,如果没有则返回false:

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

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
}

This will be called recursively until a first letter in a string is approached, otherwise returns 'no letters' . 这将被递归调用,直到接近字符串中的第一个字母,否则返回'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'));
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");
        }
    });
}

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: 公然窃取@ Lapple的测试用例:

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

// lower upper none upper upper

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM