2014-09-02 36 views
2

我想檢測我的字符串的第一個字母。如何檢查字符串的第一個字符是iOS中的字母或特定符號?

我限制,-,_0-9用戶不能首先輸入UITextField。但是假設用戶條目abc.4c並且在之前設置了交叉詞。並刪除abc,那麼字符串將是.4c &當用戶單擊提交按鈕時,其未驗證。那麼我如何檢查第一個字母是我的字符串中的一個字母或特定符號?

+0

有你經歷https://developer.apple.com/library/ios/的第一個字母文檔/ uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html – 2014-09-02 11:00:15

+0

@iosDev是否可以刪除字符串中的所有特殊字符和數字..?您的要求全部填寫。 – ilesh 2014-09-02 11:00:59

+0

您可以使用regx – BHASKAR 2014-09-02 11:04:13

回答

3

我1周:) &之前遇到這個問題我是能解決的。我分享我的代碼請檢查

NSRange textfieldfirstText = [yourtextfield.text rangeOfComposedCharacterSequenceAtIndex:0]; 
NSRange textfieldmatchText = [yourtextfield.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:textfieldfirstText]; 

if (textfieldmatchText.location != NSNotFound) 
{ 
    NSLog(@"checking..."); 
} 
else 
{ 
    NSLog(@"not checking..."); 
} 

請檢查與此。我認爲這對你有用。

+0

嗨SR Nayak。十分感謝。它的工作正常,我解決了我的問題。所以我也給你投票並接受你的回答:) – 2014-09-02 11:42:32

+0

謝謝。享受你的編碼:) – 2014-09-02 11:43:25

+0

我認爲你需要第一個字符不應該。 - _ 0 1 2 3 4 5 6 7 8 9但上面的代碼是不是檢查這個 – BHASKAR 2014-09-02 11:58:36

2
[@"your_string" characterAtIndex:0]; 

,或者你可以用正則表達式的嘗試:

NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", @"^[0-9_\.-]"]; 
BOOL isConformingToRegex = [p evaluateWithObject:@"your_string"]; 
+0

這不是工作夥計。總是去其他部分。 NSPredicate * p = [NSPredicate predicateWithFormat:@「SELF MATCHES [c]%@」,@「regex_string」]; BOOL isValid = [p evaluateWithObject:emailField.text]; (有效) if(isValid) { printf(「valid string ...」); } else { printf(「Invalid string ...」); } – 2014-09-02 11:38:17

+0

編輯我的答案。 PS:如果你正在尋找類似電子郵件的字符串檢查,我會建議這是正則表達式字符串:(?:[a-z0-9!#$%\\&'* +/=?\\^_'{| }〜 - ] +(?!?\\ [A-Z0-9#$%&\\'* +/= \\^_'{|}〜 - ] +)* | \「(?: [\\ X01 - \\ X08 \\ X0B \\ X0C \\ x0e - \\ X1F \\ X21 X23 \\ - \\ x5b \\ X5D - \\ 0x7F部分] | \\\\ [\\ x01- \ \ X09 \\ X0B \\ X0C \\ x0e - \\ 0x7F部分])* \「)@(:(:[α-Z0-9](:???[A-Z0-9 - ] * [A- Z0-9])\\)+ [A-Z0-9](?:???[A-Z0-9 - ] * [A-Z0-9])| \\ [(:(?: 25 [0-5] | 2 [0-4] [0-9] |?。[01] [0-9] [0-9])\\){3}(?: 25 [0-5] | 2 [0-4] [0-9] | [01] [0-9] [0-9] |?[A-Z0-9 - ] * [A-Z0-9] :(?:[\\ X01 - \\ X08 \\ X0B \\ X0C \\ x0e - \\ X1F \\ X21 - \\ X5A \\ X53 - \\ 0x7F部分] | \\\\ [\\ X01 - \\ X09 \\ x0b \\ x0c \\ x0e - \\ x7f])+)\\]) – grimdox 2014-09-02 12:00:19

2

您可以使用此檢查

if(textField.text.length>0){ 
    NSString *newStr = [textField.text substringToIndex:1]; 
    if ([newStr intValue]>0 || [newStr isEqualToString:@"."] || [newStr isEqualToString:@"0"] || [newStr isEqualToString:@"-"] || [newStr isEqualToString:@"_"]) { 
     NSLog(@"First charecter is wrong"); 
    } 
} 
+0

我的要求是第一個字符不應該是。 - _ 0 1 2 3 4 5 6 7 8 9 – 2014-09-02 11:18:52

1

NSString * str = @「%bc%sa」;

NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"] invertedSet]; 

NSString *Strq = [str substringToIndex:1]; 

BOOL valid = [[Strq stringByTrimmingCharactersInSet:notAllowedChars] isEqualToString:@""]; 
if (valid) { 
    // Its a Symbol 
} 
else 
{ 
    // letter or numbers 
} 
0

試試這能帶給你只能從整個字符串

NSString *codeString; 
NSString *firstLetter = [[codeString substringFromIndex:0] substringToIndex:1]; 

if([firstLetter isEqualToString:@"."]) 
{ 
NSLog(@"Wrong character to start with..."); 
} 
相關問題