快速且轻松地将模板应用于目标字符串。
这个库允许你创建轻量级模板,然后随意将它们应用到字符串实例。你还可以传递选项来定制应用模板的严格程度。
// Create a template:
let nanpPhoneNumber = String.Template(
placeholderToken: "X",
template: "(XXX) XXX-XXXX"
)
// And apply it:
let formatted = try? "5125550001".applying(template: nanpPhoneNumber)
print(formatted) // Optional("(512) 555-0001")
// Allow for partial application:
let partial = try? "512555".applying(template: nanpPhoneNumber, options: [.allowPartial])
print(partial) // Optional("(512) 555-")
// Allow for overflow application:
let overflow = try? "5125550001111111111".applying(template: nanpPhoneNumber, options: [.allowOverflow])
print(overflow) // Optional("(512) 555-0001")
// Mutate the string directly:
var str = "5125550001"
try? str.apply(template: nanpPhoneNumber)
print(str) // "(512) 555-0001"
查看测试以获取更多示例。
将此项目添加为 Package.swift
文件中的依赖项
dependencies: [
.package(url: "https://github.com/square/StringTemplate.git", .from("1.0.0")),
]
将以下内容添加到你的 Cartfile
github "square/StringTemplate"
然后运行 carthage update
。
按照Carthage 的 README中的最新说明进行操作,以获取最新的安装说明。
将以下内容添加到你的 Podfile
pod 'StringTemplate'
你还需要确保你选择使用框架
use_frameworks!
然后使用 CocoaPods 0.36 或更高版本运行 pod install
。
我想如果那是你喜欢的方式,你可以这样做。
将此仓库添加为子模块,并将项目文件添加到你的工作区。然后,你可以链接到你的应用程序目标的 StringTemplate.framework
。
我们热爱我们的贡献者!在提交拉取请求之前,请阅读我们的贡献指南。
Copyright 2017 Square, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.