Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- Human interface guide
- Protocol
- Xcode
- rxswift
- ribs
- collectionview
- uiscrollview
- SWIFT
- tableView
- clean architecture
- UICollectionView
- 스위프트
- 리팩토링
- scrollview
- 클린 코드
- 리펙토링
- UITextView
- map
- Refactoring
- RxCocoa
- swift documentation
- uitableview
- MVVM
- 애니메이션
- combine
- Clean Code
- HIG
- swiftUI
- ios
- Observable
Archives
- Today
- Total
김종권의 iOS 앱 개발 알아가기
[swift - algorithm] 문자열 치환, replacingOccurrences(of:with:), replacingCharacters(in:with:) 본문
알고리즘/문자열 처리
[swift - algorithm] 문자열 치환, replacingOccurrences(of:with:), replacingCharacters(in:with:)
jake-kim 2021. 3. 11. 02:42특정 문자열에 해당하는 곳의 문자열 치환
- replacingOccurrences(of:with:) 이용: self에서 of 문자열 부분을 with으로 변경
let sampleStr = "12345"
let newStr = sampleStr.replacingOccurrences(of: "2", with: "vv")
print(newStr) // 1vv345
Range값을 이용한 문자열 치환
- replacingCharacters(in:with:) 이용: self에서 in 범위 부분을 with으로 변경
- NSRange 개념: location, length 정보 가지고 있는 구조체

- Range 개념: lowerBound, upperBound정보를 가지고 있는 구조체

- range값을 가지고 문자열을 치환하는 예제) - textField(:shouldChangeCharactersIn:replcementString:)
* textField 델리게이트에서 현재 input창에 입력된 값을 구하고 싶은 경우
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text,
let textRange = Range(range, in: text) else {
return false
}
let updatedText = text.replacingCharacters(in: textRange, with: string)
print(updatedText)
}'알고리즘 > 문자열 처리' 카테고리의 다른 글
| [swift - algorithm] 문자열 자르기, substring (0) | 2021.03.14 |
|---|---|
| [swift - algorithm] 문자열 파싱, split(separator:), components(separatedBy:), joined() (0) | 2021.03.13 |
| [swift - algorithm] 갯수만큼 특정 문자열 추가, String(repeating:count:) (0) | 2021.03.13 |
| [swift - algorithm] Date(), 날짜, 시간 (0) | 2020.04.22 |
Comments
