ios - 我该如何分隔这样的字符串?

标签 ios objective-c nsstring tokenize

我得到一个 NSString,其中包含连接在一起的不同电子邮件,例如

def_ghi@hotmail.com_abc_1@me.com

每封电子邮件都由下划线分隔。问题是如果我尝试使用下划线字符分隔字符串,它也会分割单个电子邮件地址,因为下划线字符也可以出现在单个电子邮件中。我的尝试给了我这个结果

def

ghi@hotmail.com

abc

1@me.com

这是我的代码

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

请帮帮我。

编辑: 问了一位学长,他告诉我应该先在字符串中搜索“@”字符。当我找到这个时,我搜索一个“_”,如果存在则替换它。因为“@”之后的第一个下划线是分隔符。然后我应该从这个位置开始并再次重复上一步。我这样做直到字符串结束。请有人帮我解决这个问题。

最佳答案

使用正则表达式的解决方案,

NSString *yourString = @"def_ghi@hotmail.com_abc_1@me.com";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@"[A-Z0-9a-z._%+-]+@[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 = @"def_ghi@hotmail.com_abc_1@me.com";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@"[A-Z0-9a-z._%+-]+@[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);

关于ios - 我该如何分隔这样的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16312927/

相关文章:

ios - NSDateFormatter 中的无效日期

iphone - 在控制台/文件中打印日志语句

ios - 无法从静态库和包中加载 subview 类和 xib

ios - AVRoutePickerView 不会拾取 overrideOutputAudioPort(.speaker) 对路由所做的更改

ios - UIScrollView 不会响应 AutoResizing

ios - 如何使用 Xcode 中的 block 进行有效调试

ios - NS字符串 : String extraction from a comma Separated NSString

iOS - 一次显示字符串数组中的两个单词

objective-c - 在 switch 语句中使用 NSString

android - Flutter:如何处理需要身份验证的屏幕?