title: Escaping Strings in Swift
body: <p>I am going to create a CSV file programmatically and would like to properly escape my strings before writing them.</p>
  
  <p>I assume I'll need to escape commas and probably need to surround each value in single or double quotes (and thus will need to escape those too). Plus any carriage return / new line constants.</p>
  
  <p>I was going to write it all myself but then found this in Objective-C and said why not just convert it, as it looks quite thorough:</p>
  
  <pre><code>-(NSString *)escapeString:(NSString *)s
  {
      NSString * escapedString = s;   
      BOOL containsSeperator = !NSEqualRanges([s rangeOfString:@","], NSMakeRange(NSNotFound, 0));
      BOOL containsQuotes = !NSEqualRanges([s rangeOfString:@"\""], NSMakeRange(NSNotFound, 0));
      BOOL containsLineBreak = !NSEqualRanges([s rangeOfString:@"\n"], NSMakeRange(NSNotFound, 0));
  
      if (containsQuotes) {
          escapedString = [escapedString stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
      }
  
      if (containsSeperator || containsLineBreak) {
          escapedString = [NSString stringWithFormat:@"\"%@\"", escapedString];
      }
  
      return escapedString;
  }
  </code></pre>
  
  <p>Before I go and convert this, however, I wanted to ask the community if there is an easier way now that we're in Swift 2. Have any interesting/new changes occurred for strings that I might want to consider in favor of "Swiftifying" the above code? I did some Googling but nothing jumped out at me and I want to really make sure I do a good job here. :-)</p>
  
  <p>Thanks!</p>