Search code examples
iosxcodeswift3

Finding new line character in a String in "Swift 3"


In my app I am receiving some incoming data from a web service. In that data some wrong values can also be received like new line characters. I want to find in response string that if it contains a new line character or not.

Before Swift 3 I was able to do it like this

string.rangeOfString("\n")) == nil)

But in Swift 3 this methods is no longer available. However substring method is available which does with the help of Range.

I want to detect if my string contains "\n" how this would be accomplished using this method in Swift 3.


Solution

  • If you just want to know if it is there and don't care where it is, string.contains("\n") will return true if it is there and false if not.