2013-05-01 76 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