-
Swift - components이용해 문자열 쪼개기developing study 2024. 1. 11. 22:12
문자열 배열이 있을 경우 예를 들어
["Welcome to Korea"] 에서 단어 별로 쪼개고 싶을 때 마땅한 함수가 떠오르지 않아서
character로 공백을 포함해 배열로 만들었었다.
그런데 components를 이용해 간단히 할 수 있었다!
components(separatedBy:)
Returns an array containing substrings from the receiver that have been divided by a given separator.func components(separatedBy separator: String) -> [String]
String을 파라미터로 받고
seperator로 String배열을 리턴해준다.
리턴값이 String이기 때문에 사용하기가 좋다는 장점이 있다.
*split도 단어로 쪼갤 수 있지만 차이점이 있어 추후 다시 공부 해서 정리해야겠다.
'developing study' 카테고리의 다른 글
Swift - CaseIterable (1) 2024.01.12 Swift- enumerated() (1) 2024.01.11 Swift - compactmap (0) 2024.01.08 Swift - reduce (0) 2024.01.04 SwiftUI - trim() (0) 2023.04.16