110

Is there a way to have a way to make a new line in swift like "\n" for java?

var example: String = "Hello World \n This is a new line"
Westink
  • 1,139
  • 2
  • 7
  • 6
  • 2
    This is correct and valid for swift as well (depends on where you'll use this string of course, in a label title, you might not get multiple lines without setting label property as well). Remove the space before the next line then you'll get what you need. – smozgur Feb 14 '16 at 01:20
  • How are you printing the line? In a UI of some kind? On a CLI? – David Hoelzer Feb 14 '16 at 01:20

6 Answers6

174

You should be able to use \n inside a Swift string, and it should work as expected, creating a newline character. You will want to remove the space after the \n for proper formatting like so:

var example: String = "Hello World \nThis is a new line"

Which, if printed to the console, should become:

Hello World
This is a new line

However, there are some other considerations to make depending on how you will be using this string, such as:

  • If you are setting it to a UILabel's text property, make sure that the UILabel's numberOfLines = 0, which allows for infinite lines.
  • In some networking use cases, use \r\n instead, which is the Windows newline.

Edit: You said you're using a UITextField, but it does not support multiple lines. You must use a UITextView.

BradzTech
  • 2,755
  • 1
  • 16
  • 21
47

Also useful:

let multiLineString = """
                  Line One
                  Line Two
                  Line Three
                  """
  • Makes the code read more understandable
  • Allows copy pasting
Pbk
  • 2,004
  • 14
  • 11
10

You can use the following code;

var example: String = "Hello World \r\n This is a new line"
Celil Bozkurt
  • 1,693
  • 16
  • 18
5

You can do this

textView.text = "Name: \(string1) \n" + "Phone Number: \(string2)"

The output will be

Name: output of string1 Phone Number: output of string2

Rawand Saeed
  • 795
  • 10
  • 13
4

"\n" is not working everywhere!

For example in email, it adds the exact "\n" into the text instead of a new line if you use it in the custom keyboard like: textDocumentProxy.insertText("\n")

There are another newLine characters available but I can't just simply paste them here (Because they make a new lines).

using this extension:

extension CharacterSet {
    var allCharacters: [Character] {
        var result: [Character] = []
        for plane: UInt8 in 0...16 where self.hasMember(inPlane: plane) {
            for unicode in UInt32(plane) << 16 ..< UInt32(plane + 1) << 16 {
                if let uniChar = UnicodeScalar(unicode), self.contains(uniChar) {
                    result.append(Character(uniChar))
                }
            }
        }
        return result
    }
}

you can access all characters in any CharacterSet. There is a character set called newlines. Use one of them to fulfill your requirements:

let newlines = CharacterSet.newlines.allCharacters
for newLine in newlines {
    print("Hello World \(newLine) This is a new line")
}

Then store the one you tested and worked everywhere and use it anywhere. Note that you can't relay on the index of the character set. It may change.

But most of the times "\n" just works as expected.

Community
  • 1
  • 1
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • `CharacterSet.newlines` includes 5 characters `(U+000A ~ U+000D, U+0085, U+2028, and U+2029)` and appending all of them makes no sense. Also scanning so many characters when it's known which are the 2 you are looking for (`\r` and `\n`) is not efficient. – timbre timbre Dec 29 '21 at 14:45
  • ‘allCharacters’ means you should append all characters. I don’t know the nonsense. Can you explain please? Also as I mentioned, those two are not working everywhere. And also you should bench mark to see if it is efficient enough or not in your use case ;) – Mojtaba Hosseini Dec 29 '21 at 17:47
  • 1
    The question was "How do I make a new line in swift". Characters `U+2028`, and `U+2029` are not making a "new line", although they do control lines and paragraphs. So both characters are irrelevant to the question. Secondly, "store the one you tested and worked everywhere" - there's no such thing. There are specific rules about which characters work where. Finally, why discover them, if they are well defined in Unicode standards (see https://www.unicode.org/versions/Unicode5.2.0/ch05.pdf#G10213)? – timbre timbre Dec 29 '21 at 19:41
-1

I have tried and this way so easier to read and convenient to copy and paste:

let exampleString = """
              Begin of string
              Line 1
              Line 2
              End of string
              """