[swift] 문자열 나누기

chosh·2023년 5월 30일
0
  1. split

    let string = "Hello,World,,Swift,Programming"
    let array = string.split(separator: ",")
    print(array) // ["Hello", "World", "Swift", "Programming"]

    서브 스트링을 반환

    var greeting = "Hello,playground"
    
    let test = greeting.split(separator: ",")
    print(type(of: test[0])) // <<<<<<<<<<<<< Substring

    서브스트링은 빈 문자열을 포함 하지 않음, 그래서 콤마가 두개여도 빈문자열이 없음

  2. components

    let string = "Hello,World,,Swift,Programming"
    let array = string.components(separatedBy: ",")
    print(array) // ["Hello", "World", "", "Swift", "Programming"]

    String 을 반환

    var greeting = "Hello,playground"
    
    let test = greeting.components(separatedBy: ",")
    print(type(of: test[0])) // <<<<<<<<<<<<<<<<<<<<<<<< String

    빈문자열 포함

profile
제가 참고하기 위해 만든 블로그라 글을 편하게 작성했습니다. 틀린거 있다면 댓글 부탁드립니다.

0개의 댓글