Search code examples
stringaxaptax++dynamics-ax-2012uppercase

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 .

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.

There is a solution?

Thanks,

Enjoy!


Solution

  • 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:

    enter image description here

    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.