How to mask a string in swift 4.2











up vote
0
down vote

favorite












I wanna make a mask in a string.






let unmasked = "12345678900"
//string masked = "123.456.789-00"





This is the braziliam CPF format, i found nothing in Stack Overflow BR










share|improve this question


























    up vote
    0
    down vote

    favorite












    I wanna make a mask in a string.






    let unmasked = "12345678900"
    //string masked = "123.456.789-00"





    This is the braziliam CPF format, i found nothing in Stack Overflow BR










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I wanna make a mask in a string.






      let unmasked = "12345678900"
      //string masked = "123.456.789-00"





      This is the braziliam CPF format, i found nothing in Stack Overflow BR










      share|improve this question













      I wanna make a mask in a string.






      let unmasked = "12345678900"
      //string masked = "123.456.789-00"





      This is the braziliam CPF format, i found nothing in Stack Overflow BR






      let unmasked = "12345678900"
      //string masked = "123.456.789-00"





      let unmasked = "12345678900"
      //string masked = "123.456.789-00"






      swift4.2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 14:05









      Maicon Santos

      93




      93
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          I found a solution using a character array and array.insert to insert mask in defined index



          Portuguese:
          Eu encontrei uma solução usando um array de caracteres e a função array.insert que permite inserir qualquer string em um determinado index da matriz.






          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)








          share|improve this answer




























            up vote
            0
            down vote













            You could use this gist in your project :-
            StringFormattor



            Usage :



            var maskedResult = unmasked.format("NNN.NNN.NNN-NN", oldString: "unmasked")





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413851%2fhow-to-mask-a-string-in-swift-4-2%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              I found a solution using a character array and array.insert to insert mask in defined index



              Portuguese:
              Eu encontrei uma solução usando um array de caracteres e a função array.insert que permite inserir qualquer string em um determinado index da matriz.






              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)








              share|improve this answer

























                up vote
                0
                down vote













                I found a solution using a character array and array.insert to insert mask in defined index



                Portuguese:
                Eu encontrei uma solução usando um array de caracteres e a função array.insert que permite inserir qualquer string em um determinado index da matriz.






                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)








                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I found a solution using a character array and array.insert to insert mask in defined index



                  Portuguese:
                  Eu encontrei uma solução usando um array de caracteres e a função array.insert que permite inserir qualquer string em um determinado index da matriz.






                  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)








                  share|improve this answer












                  I found a solution using a character array and array.insert to insert mask in defined index



                  Portuguese:
                  Eu encontrei uma solução usando um array de caracteres e a função array.insert que permite inserir qualquer string em um determinado index da matriz.






                  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)








                  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)





                  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)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 at 14:53









                  Maicon Santos

                  93




                  93
























                      up vote
                      0
                      down vote













                      You could use this gist in your project :-
                      StringFormattor



                      Usage :



                      var maskedResult = unmasked.format("NNN.NNN.NNN-NN", oldString: "unmasked")





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You could use this gist in your project :-
                        StringFormattor



                        Usage :



                        var maskedResult = unmasked.format("NNN.NNN.NNN-NN", oldString: "unmasked")





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You could use this gist in your project :-
                          StringFormattor



                          Usage :



                          var maskedResult = unmasked.format("NNN.NNN.NNN-NN", oldString: "unmasked")





                          share|improve this answer












                          You could use this gist in your project :-
                          StringFormattor



                          Usage :



                          var maskedResult = unmasked.format("NNN.NNN.NNN-NN", oldString: "unmasked")






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 at 11:22









                          Ajay Singh Mehra

                          363112




                          363112






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413851%2fhow-to-mask-a-string-in-swift-4-2%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Berounka

                              Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

                              Sphinx de Gizeh