2013-05-01 73 views
0

我得到含串聯在一起,就像如何分離這樣的字符串?

def_ghi @ hotmail.com_abc_1 @ me.com

每個郵件是由underscore.The問題分開是不同的郵件一個NSString如果我試圖分開字符串使用下劃線字符,它也可以將單個電子郵件地址細分爲下劃線字符也可以在單個電子郵件中。我已經試過給了我這樣的結果

高清

[email protected]

ABC

[email protected]

這裏是我的代碼

NSString *string = //The string I am receiving. 
NSArray *chunks = [string componentsSeparatedByString: @"_"]; 

請幫幫我。

編輯: 我問一個資深的,他告訴我,我應該先搜索字符串「@」 character.When我發現這一點,那麼我尋找一個「_」,如果它存在更換。作爲「@」之後的第一個下劃線是分隔符。我應該從這個位置開始,然後重複上一步。我這樣做直到字符串結束。請有人幫我解決這個問題。

+2

你不能做到這一點。您可能需要一個不能是值的一部分的分隔符,或者如果它出現在值中,則必須轉義分隔符。否則,無法知道如何正確拆分字符串。 – rmaddy 2013-05-01 04:56:14

+0

我問了一個老大,他告訴我,我應該首先搜索字符串「@」字符。當我找到這個,然後我搜索一個「_」,並將其替換,如果它存在。作爲「@」後的第一個下劃線是分隔符。然後我應該從這個位置開始,然後重複上一步驟。直到字符串結束爲止。 – Jpk 2013-05-01 05:01:16

+2

好的,但我不明白爲什麼這比它應該更難。如果將分隔符更改爲無法顯示在有效電子郵件地址中的字符,對於每個人來說,這會更有意義。 – rmaddy 2013-05-01 05:05:44

回答

3

解決方案使用正則表達式嘗試,

NSString *yourString = @"[email protected][email protected]"; 
NSError *error = NULL; 
NSRegularExpression *regex = [NSRegularExpression 
           regularExpressionWithPattern:@"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" 
           options:NSRegularExpressionCaseInsensitive 
           error:&error]; 
[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ 

    // detect email addresses 
    NSString *email = [yourString substringWithRange:match.range]; 

    //this part remove the '_' between email addresses 
    if(match.range.location != 0){ 
     if([email characterAtIndex:0]=='_'){ 
      email = [email substringFromIndex:1]; 
     } 
    } 

    //print the email address 
    NSLog(@"%@",email); 

}]; 

編輯:如何收集它們,

聲明這樣的變量,

@property(nonatomic, strong) NSMutableArray *emailsArray; 



_emailsArray = [[NSMutableArray alloc] init]; 

NSString *yourString = @"[email protected][email protected]"; 
NSError *error = NULL; 
NSRegularExpression *regex = [NSRegularExpression 
           regularExpressionWithPattern:@"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" 
           options:NSRegularExpressionCaseInsensitive 
           error:&error]; 
[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ 

    // detect email addresses 
    NSString *email = [yourString substringWithRange:match.range]; 

    //this part remove the '_' between email addresses 
    if(match.range.location != 0){ 
     if([email characterAtIndex:0]=='_'){ 
      email = [email substringFromIndex:1]; 
     } 
    } 

    //print the email address 
    NSLog(@"%@",email); 
    [self.emailsArray addObject:email]; 
}]; 


NSLog(@"%@",self.emailsArray); 
然後只
+1

+1完美完成 – 2013-05-01 05:16:03

+0

@Thilina如何在電子郵件之間添加關於下劃線的檢查?請編輯您的代碼。我不瞭解正則表達式。 – Jpk 2013-05-01 05:20:52

+0

@Jimmy Johnny,我更改了代碼,並添加了該部分。只需複製代碼並運行它。歡呼聲 – 2013-05-01 05:21:51

0

NSString *string = @"[email protected][email protected]"; 
NSArray *stringComponents = [string componentsSeparatedByString:@"_"]; 

NSMutableString *mutableString = [NSMutableString string]; 
NSMutableArray *emailIDs = [NSMutableArray array]; 
for (NSString *component in stringComponents) { 
    if (!mutableString) { 
     mutableString = [NSMutableString string]; 
    } 
    [mutableString appendFormat:@"_%@",component]; 

    if ([component rangeOfString:@"@"].location != NSNotFound) { 
     [emailIDs addObject:[mutableString substringFromIndex:1]]; 
     mutableString = nil; 
    } 
} 

NSLog(@"%@",emailIDs); 
0

鑑於您的要求這樣的事情應該爲你工作:

NSString *string = @"[email protected][email protected]"; 
NSMutableArray *addresses = [NSMutableArray array]; 
NSUInteger currentIndex = 0; // start from beginning 
// Stop when we are past the end of the string 
while (currentIndex < string.length) { 
    // Find the next @ symbol 
    NSRange atRange = [string rangeOfString:@"@" options:0 range:NSMakeRange(currentIndex, string.length - currentIndex)]; 
    if (atRange.location != NSNotFound) { 
     // We found another @, not look for the first underscore after the @ 
     NSRange underRange = [string rangeOfString:@"_" options:0 range:NSMakeRange(atRange.location, string.length - atRange.location)]; 
     if (underRange.location != NSNotFound) { 
      // We found an underscore after the @, extract the email address 
      NSString *address = [string substringWithRange:NSMakeRange(currentIndex, underRange.location - currentIndex)]; 
      [addresses addObject:address]; 
      currentIndex = underRange.location + 1; 
     } else { 
      // No underscore so this must be the last address in the string 
      NSString *address = [string substringFromIndex:currentIndex]; 
      [addresses addObject:address]; 
      currentIndex = string.length; 
     } 
    } else { 
     // no more @ symbols 
     currentIndex = string.length; 
    } 
} 

NSLog(@"Addresses: %@", addresses); 
1

這裏就如何重新構建從你發現自己與有些凌亂的字符串的郵件地址的原始名單有很多很好的答案。

我想提出一個NSScanner基礎的解決方案,這似乎是非常適合:

NSString *messyString = @"[email protected][email protected]"; 

NSScanner *mailScanner = [NSScanner scannerWithString:messyString]; 

NSMutableArray *mailAddresses = [NSMutableArray array]; 

while (YES) { 

    NSString *recipientName; 
    NSString *serverName; 
    BOOL found = [mailScanner scanUpToString:@"@" intoString:&recipientName]; 
    found |= [mailScanner scanUpToString:@"_" intoString:&serverName]; 
    if (!found) break; 

    [mailAddresses addObject:[recipientName stringByAppendingString:serverName]]; 

    // Consume the delimiting underscore 
    found = [mailScanner scanString:@"_" intoString:nil]; 
    if (!found) break; 
} 
+0

由於某種原因,我總是忘記'NSScanner'。這比我的代碼簡單得多。兩個拼寫錯誤 - 1)它是'found | ='。 2)'while'塊末尾不需要分號。 – rmaddy 2013-05-01 05:36:43

+0

+1它根據需要工作..... – 2013-05-01 06:17:14

+0

感謝您在幾個問題中使用可可片:) – 2013-05-01 06:28:27