2015-06-16 93 views
0

我需要知道一个字符串是否至少有一个或多个字符。我需要找到uppercase字符。如何查找字符串中是否存在大写字符?

我用这个代码:

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

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

可是不行的!

一个问题是,功能strScan不区分大写和小写。

有解决方案吗?

谢谢,

享受!

回答

3

下面测试的代码,如果一个字符串是一个字符以上,之后发现所有的大写字符。数字被忽略,因为它们不能是大写。

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)); 
     } 
    } 
} 

这是输出:

enter image description here

编辑:像扬B.杰德森指出,使用char2num(testStr, i) != char2num(strLwr(testStr), i)而不是char2num(testStr, i) == char2num(strUpr(testStr), i),以确保它正确地评估符号和数字。

+1

这对于非字母数字字符不起作用。 更好的是测试差异小写:'char2num(testStr,i)!= char2num(strLwr(testStr),i)' –

+0

感谢所有的hrlp! – ulisses

+0

@ JanB.Kjeldsen - 哇!你的权利!我想到了数字,并使用str2IntOk()忽略它们,但没有考虑像“!”这样的符号。如果我理解正确,关键是测试NOT EQUALS,因为数字或符号的大写值与小写字母的值相同。如果您测试大写和小写值不相同,则您知道该字符是字母而不是数字或符号。感谢您指出! –

4

这是我写的一个工作,它显示了3种比较字符串与区分大小写的不同方法。只需复制/粘贴/运行。

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)); 
    } 
} 
+0

非常感谢@AlexK! 我真的很感谢你的帮助! – ulisses

相关问题