2016-01-19 114 views
-1

我需要修復代碼的幫助。 我需要它來查看子類的前兩個字符。如果是字母數字(字母和數字)subclassize=7;如果前兩個字符都是數字 subclassize=4。 這是代碼:字符串中的字母數字

int startrentex=0; 
int rentexsize=0; 
//int totalsize=0; 
int subclasssize=7; 
//int descriptionsize=9; 
int currentlength=[enteredText length]; 
if(appdata.appSettings.BarCodeStyle==0) { 
    if(currentlength>1){ 

     NSString *str = [enteredText substringWithRange:NSMakeRange(1, 1)]; 
     int testint=[str intValue]; 
     if(![str isEqualToString:@"0"] && testint==0) 
      subclasssize=7; 
     else 
      subclasssize=4; 
     //a3fs12345hi there 
     //1B3456712345hi there 
    } 
    startrentex=subclasssize; 
    if(currentlength>subclasssize) 
    { 
     BOOL isspace=YES; 
+0

歡迎來到Stack Overflow。請儘快閱讀[關於]頁面。這是Objective C的代碼,不是嗎?大量使用''''強烈表明,無論如何它不是C++。如果你使用了正確的標籤([tag:objective-c],如果我的假設是正確的),你會得到更好的答案。 –

+0

無關注 - 請使用鍵盤上的空格鍵。 「這是更容易閱讀?」或「Isthiseasiertoread?」代碼也一樣。 'if(currentLength> subclasssize)'比'if(currentLength> subclasssize)'更容易。 – rmaddy

+0

您需要告訴我們當前代碼有什麼問題,否則問題不完整。 – Cristik

回答

1

我需要它來看看子類的前兩個字符。

如果你想看看字符然後使用NSString方法- characterAtIndex:。這將返回一個類型爲unichar的值 - Objective-C中的unicode字符的類型。

一旦你有你的前兩個字符,你可以測試它們。

如果是字母數字(字母和數字)subclassize=7;如果前兩個字符都是數字subclassize=4

要確定一個字符的類,您可以使用NSCharacterSet。這個課程提供了標準的字符集,例如+ decimalDigitCharacterSet,以及測試成員資格的方法- characterIsMember:

有了這些類和方法,您應該能夠快速解決您的問題。您將在Apple的文檔中找到這些類和方法的全部細節。

HTH

相關問題