2

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.

Mohammad Aamir
  • 471
  • 2
  • 5
  • 14
  • extension String { var lines: [String] { var result: [String] = [] enumerateLines { line, _ in result.append(line) } return result } } – Himanshu Moradiya Jun 09 '17 at 05:50
  • let text = "line1\nline2" let array = text.components(separatedBy: CharacterSet.newlines) use this things. – Himanshu Moradiya Jun 09 '17 at 05:52
  • please check this answer https://stackoverflow.com/questions/39677330/how-does-string-substring-work-in-swift-3 – LZ041 Jun 09 '17 at 05:52
  • @HimanshuMoradiya I received a new Line Character in response in whole when wrong value is received. So, I just want to detect if it's a new line or not. – Mohammad Aamir Jun 09 '17 at 05:54
  • then text.components(separatedBy: CharacterSet.newlines) in this you got it . and ya if you want only for check new line availble or not then check array.count > 0 that means new line or else part no new line – Himanshu Moradiya Jun 09 '17 at 05:55

6 Answers6

4

Short answer for Swift 5+

You can use

string.contains { $0.isNewline }

or KeyPath based syntax

string.contains(where: \.isNewline)

to detect if string contains any newline character.


Long answer

Swift 5 introduced couple of new properties on Character. Those simplify such tests and are more robust then simple check for \n.

Now you can use

myCharacter.isNewline

For complete list check Inspecting a Character section in Character docs

Example:

Character("\n").isNewline // true
Character("\r").isNewline // true
Character("a").isNewline  // false

Lukas Kukacka
  • 7,604
  • 2
  • 25
  • 50
3

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.

Nate Birkholz
  • 2,739
  • 4
  • 20
  • 29
2

You can also use

yourString.rangeOfCharacter(from: CharacterSet.newlines) != nil

which is more elegant as it's not using harcoded newline character string

Thibaud David
  • 496
  • 2
  • 11
1

Swift 3

string.range(of: "\n")

To check:

if string.range(of: "\n") == nil{

}

Or if you simply want to check the string contains \n or not, Then,

if !str.characters.contains("\n") {

}
Maddy
  • 1,660
  • 11
  • 24
0

.characters.contains() should do the trick:

let s1 = "Hi I'm a string\n with a new line"
s1.characters.contains("\n")  // => true

let s2 = "I'm not"
s2.characters.contains("\n")  // => false
LimeRed
  • 1,278
  • 13
  • 18
0
let string:String = "This is a string"
if string.range(of: "\n") == nil {
    print  ("contains nil")
}
else {
    print("contains new line")
}

perferctly working in swift 3.

elk_cloner
  • 2,049
  • 1
  • 12
  • 13