简体   繁体   English

如何查找字符串中是否存在大写字符?

[英]How to find if exist an uppercase character in string?

I need to know if a string is at least one character or more. 我需要知道一个字符串是否至少一个字符或更多。 I need to find the uppercase character . 我需要找到uppercase字符。

I used this code: 我使用以下代码:

str testStr;
int flag;
testStr = "abdE2" ;

flag = strScan(testStr , "ABCDEFGHILMNOPQRSTUVZ" ,flag ,strLen(testStr));
info(strFmt("%1",flag) );

But not work! 但是不行!

A problem is that the function strScan does not distinguish uppercase and lowercase. 问题在于函数strScan无法区分大写和小写。

There is a solution? 有解决办法吗?

Thanks, 谢谢,

Enjoy! 请享用!

Here is a job I wrote that shows 3 different methods of comparing strings with case sensitivity. 这是我写的一份工作,它显示了三种比较区分大小写的字符串的方法。 Just copy/paste/run. 只需复制/粘贴/运行。

static void Job86(Args _args)
{
    str a = 'Alex';
    str b = 'aleX';
    int i;
    int n;
    str     c1, c2;


    setPrefix("Compare");
    for (n=1; n<=strLen(b); n++)
    {
        c1 = subStr(a, n, 1);
        c2 = subStr(b, n, 1);

        if (char2num(c1, 1) == char2num(c2, 1))
            info(strFmt("Char2Num()\t%1 == %2", c1, c2));
        else
            info(strFmt("Char2Num()\t%1 != %2", c1, c2));


        if (strCmp(c1, c2) == 0)
            info(strfmt("strCmp()\t%1 == %2", c1, c2));
        else
            info(strFmt("strCmp()\t%1 != %2", c1, c2));

        i = System.String::Compare(c1, c2);

        if (i == 0)
            info(strfmt("System.String::Compare()\t%1 == %2", c1, c2));
        else
            info(strFmt("System.String::Compare()\t%1 != %2", c1, c2));
    }   
}

The code below tests if a string is one character or more and afterwards finds all the uppercase characters. 下面的代码测试字符串是否是一个或多个字符,然后查找所有大写字符。 Numbers are ignored since they cannot be uppercase. 数字将被忽略,因为它们不能为大写。

static void findCapitalLetters(Args _args)
{
    str testStr = "!#dE2";
    int i;
    int stringLenght = strLen(testStr);
    str character;

    //Find out if a string is at least one character or more
    if (stringLenght >= 1)
    {
        info(strFmt("The string is longer than one character: %1",stringLenght));
    }

    //Find the uppercase character (s)
    for (i=1; i<=stringLenght; i+=1)
    {
        character = subStr(testStr, i, 1);

        if (char2num(testStr, i) != char2num(strLwr(testStr), i))
        {
            info(strFmt("'%1' at position %2 is an uppercase letter.", character, i));
        }
    }  
}

This is the output: 这是输出:

在此处输入图片说明

EDIT: Like Jan B. Kjeldsen pointed out, use char2num(testStr, i) != char2num(strLwr(testStr), i) and not char2num(testStr, i) == char2num(strUpr(testStr), i) to make sure it evaluates symbols and numbers correctly. 编辑:就像Jan B. Kjeldsen指出的那样,使用char2num(testStr, i) != char2num(strLwr(testStr), i)而不是char2num(testStr, i) == char2num(strUpr(testStr), i)以确保它可以正确评估符号和数字。

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

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