简体   繁体   中英

Filter new line from array of email strings

I'm trying to send emails to a list that I get from a server which is an array of emails whose output is in this format

(
    "john@gmail.com\n",
    "katebell@gmail.com\n"
     "\nakhil@gmail.com",
     "mary@gmail.com",
     "timcorb\n@gmail.com
)

Now as you can see some emails have newline characters in between and those emails doesnt get sent. I'm trying to find an efficient way to filter out those newlines, my current approach is to loop through all emails and check for newline in each email and if newline exist replace it with a null string. Is there a better way to do this or should I just stick with that? Also Will my current approach cause any issues in any other scenarios?

try something like this

NSString *fileName = @"\ntest\n";  
fileName = [fileName stringByReplacingOccurrencesOfString:@"\n" withString:@""];

eg.

   NSString * str = @"timcorb\n@gmail.com";
   str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
   NSLog(@"%@",str);

it will Log 2014-01-10 01:01:00.256 demo[26220] timcorb@gmail.com

您可以使用以下代码替换字符串中的字符。


    NSString *email = @"\nakhi\nl@gmail.com";
NSString *actualEmail = [email stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSMutableArray* emailArray = [[NSMutableArray alloc] init];

for (int _index = 0; _index < [yourArray count]; _index++) {
  [emailArray addobject:[[yourArray objectAtIndex:_index]  stringByReplacingOccurrencesOfString:@"\n" withString:@""]];
}

This will give you your email array

One way you can try using NSRegularExpression like this below :-

    NSArray *array=@[@"john@gmail.com\n",@"katebell@gmail.com\n",@"\nakhil@gmail.com",@"mary@gmail.com",@"timcorb\n@gmail.com"];
    NSString *string =[array componentsJoinedByString: @","];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\n" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
    NSLog(@"%@",modifiedString);

Output:-

john@gmail.com,katebell@gmail.com,akhil@gmail.com,mary@gmail.com,timcorb@gmail.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM