2012-03-26 102 views
1

我已經搜索和搜索,並且由於在iOS中字符串中使用了「@」符號(例如:@「mystring」),我無法找到如何確定字符串包含它。一旦我確定它是否包含它,我需要知道它在字符串中的位置。如何查找在iOS中包含@符號的子字符串

NSArray *array_Email_Parts = [str_Email_Address componentsSeparatedByString: @"//@"]; 

以上不起作用。我已經嘗試了它的幾個變種。有什麼建議麼?

+1

您可以用反斜槓轉義的特殊字符,所以也許嘗試'@ 「\ @」' – joshuahealy 2012-03-26 22:40:16

+0

可能重複的[的NSString的indexOf在Objective-C](http://stackoverflow.com/questions/256460/的NSString-的indexOf合目標c) – 2012-03-26 22:47:24

回答

4

這將得到@符號的索引。

NSString *email = @"[email protected]"; 
NSRange range = [email rangeOfString:@"@"]; 
if (range.location != NSNotFound) { 
    NSLog(@"Location of @ is %i", range.location); 
    // Prints "Location of @ is 4" 
} 

注意@NSString內完全有效的,不要逃避它。

NSArray *parts = [email componentsSeparatedByString:@"@"]; 
if ([parts count] == 2) { 
    NSString *user = [parts objectAtIndex:0]; // equal to "user" 
    NSString *domain = [parts objectAtIndex:1]; // equal to "example.com" 
} 
1

我真的不明白你是什麼其實是問,但我認爲你正在尋找這樣的:

if ([myString rangeOfString:@"@"].location != NSNotFound) 
    //myString contains @ 
else 
    //myString does not contain @ 

如果您有字符串中的@麻煩嘗試@@

相關問題