[Swift] How to replace multiple strings

How to replace multiple strings

Before using replacingOccurrences (of: with :) If you create a dictionary, you can "replace multiple character strings".

code

var str = "ABCDEF"
var newStr: String {
    let dictionary = ["A": "1","B": "2","C": "3","D": "4","E": "5","F": "6"]
    return dictionary.reduce(str) { $0.replacingOccurrences(of: $1.key, with: $1.value) }
}
print(newStr)  // 123456

Recommended Posts

[Swift] How to replace multiple strings
How to concatenate strings
[Swift] How to use UserDefaults
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift] How to send a notification
How to concatenate strings in java
[Swift] How to fix Label in UIPickerView
[Swift] How to use Tab Bar Controller
How to call Swift 5.3 code from Objective-C
[Swift] How to implement the countdown function
[Swift5] How to implement standby screen using'PKHUD'
[Swift5] How to implement animation using "lottie-ios"
How to overwrite Firebase data in Swift
[Swift] How to use one option alert
How to switch between multiple Java versions
How to execute multiple commands in docker-compose.yml
How to deploy
[java] Summary of how to handle character strings
How to save to multiple tables with one input
[Swift] How to implement the LINE login function
Note how to use Swift super basic TableView
[swift5] How to implement the Twitter share function
[Swift] How to link the app with Firebase
How to create multiple pull-down menus with ActiveHash
How to create search conditions involving multiple models
[Rails] How to upload multiple images using Carrierwave
How to install multiple JDKs on Ubuntu 18.04 LTS
[Swift UI] How to disable ScrollsToTop of ScrollView
How to use the replace () method (Java Silver)
How to execute tasks in parallel in Swift in Swift Package
How to define multiple orm.xml in Spring4, JPA2.1
[Java] How to make multiple for loops single
[Ruby] When you want to replace multiple characters
How to implement UICollectionView in Swift with code only
How to change the action with multiple submit buttons
8 Corresponds to multiple arguments
How to develop OpenSPIFe
[Rails] How to search by multiple values ​​with LIKE
[Swift] How to play songs from your music library
How to call AmazonSQSAsync
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to write dockerfile
How to uninstall Rails
How to install docker-machine
[How to use label]
How to make shaded-jar
How to write docker-compose
How to use identity