Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Is there any way to extract/omit certain characters from a string for a readable version?

SwiftQuestion

My questions is - Is it possible to extract characters from a variable/array so that it tweets/shares/emails in a readable format

I.E The code I have is like this:

// var currentQuote = "\"Silence is better than Bullsh!t\"\n - Unknown"

And I want to be able to have my Share function Tweet/Email the currentQuote however, It emails it with the characters "\" and "\n included, Is there anyway I can set a rule to extract these when sharing/tweeting it?

I.E) It would tweet something like this:

// Silence is better than Bullsh!t - Unknown

Your answers are very much appreciated,

Jack

1 Answer

Dominic Bryan
Dominic Bryan
14,452 Points

Okay so I am not sure how you are retrieving this data but that is something you will have to work out but this is what I would do. It involves NSUTF8Encoding and if you put this in your project and just use the println() ive written to, you will see its results, of which I will write below:

var currentQuote = "\"Silence is better than BullSh!t\"\n - Unknown"

let decodedData = currentQuote.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: decodedData, options: attributedOptions, documentAttributes: nil, error: nil)
let undecodedQuote = attributedString?.string
println(undecodedQuote!)

Result: "Silence is better than BullSh!t" - Unknown

Hope this helps, just comment if you would like some clarification. Please don't forget to mark this as answer if it works for you so others know there is an answer (and it helps me out too ) :D

Thanks

Dom