{"id":582,"date":"2018-03-01T21:24:49","date_gmt":"2018-03-01T21:24:49","guid":{"rendered":"http:\/\/www.45rpmsoftware.com\/wordpress\/?p=582"},"modified":"2018-03-22T11:13:31","modified_gmt":"2018-03-22T11:13:31","slug":"nsstring-componentsseparatedbycaseinsensitivestring","status":"publish","type":"post","link":"https:\/\/www.45rpmsoftware.com\/blog\/?p=582","title":{"rendered":"NSString &#8211; components separated by case insensitive string"},"content":{"rendered":"<p>The latest version of MailRaider greatly improves its handling of base64 encoded or quoted printable strings &#8211; the previous version could get confused if, for example, it encountered &#8216;=?UTF-8?B?&#8217; instead of &#8216;=?utf-8?b?&#8217;. The problem is that NSString only has componentsSeparatedByString &#8211; which isn&#8217;t much use if you don&#8217;t know what case to expect. Hmm.<!--more--><\/p>\n<p>To solve this problem, I wrote my own componentsSeparatedByCaseInsensitiveString &#8211; it works in exactly the same way as componentsSeparatedByString, and it returns the same result, with the exception that it&#8217;s case insensitive. Hence the name.<\/p>\n<p>Feel free to use it in your own code &#8211; no attribution necessary. All I ask is that, if you can improve this code (perhaps by making it faster and more efficient) then you share your improvements in the comments below.<\/p>\n<p>The latest version of MailRaider greatly improves its handling of base64 encoded or quoted printable strings &#8211; the previous version could get confused if, for example, it encountered &#8216;=?UTF-8?B?&#8217; instead of &#8216;=?utf-8?b?&#8217;. The problem is that NSString only has componentsSeparatedByString &#8211; which isn&#8217;t much use if you don&#8217;t know what case to expect. Hmm.<\/p>\n<p>To solve this problem, I wrote my own componentsSeparatedByCaseInsensitiveString &#8211; it works in exactly the same way as componentsSeparatedByString, and it returns the same result, with the exception that it&#8217;s case insensitive. Hence the name.<\/p>\n<p>Feel free to use it in your own code &#8211; no attribution necessary. All I ask is that, if you can improve this code (perhaps by making it faster and more efficient) then you share your improvements in the comments below.<\/p>\n<pre>@implementation NSString (String_Wangers)\r\n\r\n- (NSArray&lt;NSString *&gt; *)componentsSeparatedByCaseInsensitiveString:(NSString *)separator {\r\n\u00a0 \u00a0 NSMutableArray* returnArray = NSMutableArray.new;\r\n\u00a0 \u00a0 NSRange searchRange = NSMakeRange(0,self.length);\r\n\u00a0 \u00a0 NSRange iRange;\r\n\u00a0 \u00a0 while (true) {\r\n\u00a0 \u00a0  \u00a0 searchRange.length = self.length - searchRange.location;\r\n\u00a0 \u00a0  \u00a0 iRange = [self rangeOfString:separator options:NSCaseInsensitiveSearch range:searchRange];\r\n\u00a0 \u00a0  \u00a0 if (iRange.location != NSNotFound) {\r\n\u00a0 \u00a0  \u00a0  \u00a0 searchRange.location = iRange.location+iRange.length;\r\n\u00a0 \u00a0  \u00a0  \u00a0 NSString* subString = [self substringWithRange:NSMakeRange(searchRange.location,self.length - searchRange.location)];\r\n\u00a0 \u00a0  \u00a0  \u00a0 NSRange jRange = [subString rangeOfString:separator options:NSCaseInsensitiveSearch];\r\n\u00a0 \u00a0  \u00a0  \u00a0 [returnArray addObject:(jRange.location != NSNotFound)?[subString substringWithRange:NSMakeRange(0,jRange.location)]:subString];\r\n\u00a0 \u00a0  \u00a0 } else {\r\n\u00a0 \u00a0  \u00a0  \u00a0 iRange = [self rangeOfString:separator options:NSCaseInsensitiveSearch];\r\n\u00a0 \u00a0  \u00a0  \u00a0 [returnArray insertObject:(iRange.location != NSNotFound)?[self substringWithRange:NSMakeRange(0,iRange.location)]:self atIndex:0];\r\n\u00a0 \u00a0  \u00a0  \u00a0 break;\r\n\u00a0 \u00a0  \u00a0 }\r\n\u00a0 \u00a0 }\r\n\u00a0 \u00a0 return returnArray;\r\n}\r\n\r\n@end<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The latest version of MailRaider greatly improves its handling of base64 encoded or quoted printable strings &#8211; the previous version could get confused if, for example, it encountered &#8216;=?UTF-8?B?&#8217; instead of &#8216;=?utf-8?b?&#8217;. The problem is that NSString only has componentsSeparatedByString &#8211; which isn&#8217;t much use if you don&#8217;t know what case to expect. Hmm.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,15,28],"tags":[],"_links":{"self":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/582"}],"collection":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=582"}],"version-history":[{"count":0,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/582\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}