如何在Swift 4.2中屏蔽字符串

时间:2018-11-21 14:05:43

标签: swift4.2

我想在一个字符串中做一个面具。

/**
 * Set the {@link ClientHttpRequestFactory} for the underlying {@link RestTemplate}.
 * @param requestFactory The request factory.
 * @return the spec
 */
public HttpMessageHandlerSpec requestFactory(ClientHttpRequestFactory requestFactory) {

这是巴西的CPF格式,我在Stack Overflow BR中找不到任何内容

2 个答案:

答案 0 :(得分:1)

您可以在项目中使用此要点:- StringFormattor

用法:

TextInput

答案 1 :(得分:0)

我找到了使用字符数组和array.insert在定义的索引中插入掩码的解决方案

葡萄牙语: 可以使用通用的数组和功能数组。插入允许插入的限定字符串,可以确定母体的索引。

let cpf = "12345678900"
var characters = Array(cpf) //making a character array (criando um array de caracteres)

characters.insert(".", at: 3) //inserting "." in index 3 (inserindo "." no index 3) 
//character = ["1","2","3",".","4","5","6","7","8","9","0","0"]

characters.insert(".", at: 7) // inserting "." in index 7 (inserindo "." no index 7)
//character = ["1","2","3",".","4","5","6",".","7","8","9","0","0"]

characters.insert("-", at: 11)// inserting "." in index 11 (inserindo "." no index 11)
//character = ["1","2","3",".","4","5","6",".","7","8","9","-","0","0"]

let masked = String(characters) //convert character to string
pint("cpf masked: ",masked)
//the masked will show: 123.456.789-00 (a mascara irá mostrar: 123.456.789-00)