[英]How to find if exist an uppercase character in string?
我需要知道一個字符串是否至少一個字符或更多。 我需要找到uppercase
字符。
我使用以下代碼:
str testStr;
int flag;
testStr = "abdE2" ;
flag = strScan(testStr , "ABCDEFGHILMNOPQRSTUVZ" ,flag ,strLen(testStr));
info(strFmt("%1",flag) );
但是不行!
問題在於函數strScan無法區分大寫和小寫。
有解決辦法嗎?
謝謝,
請享用!
這是我寫的一份工作,它顯示了三種比較區分大小寫的字符串的方法。 只需復制/粘貼/運行。
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));
}
}
下面的代碼測試字符串是否是一個或多個字符,然后查找所有大寫字符。 數字將被忽略,因為它們不能為大寫。
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));
}
}
}
這是輸出:
編輯:就像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.