Replace fixed number of symbols in String












-1















I have this number: 4200000000000000
I would like to leave only the first 4 digits and last 3 digits:



42000......000


Everything else should be replaced by dots. How I can implement this with some smart algorithm?










share|improve this question























  • Is the input a String? And I'm assuming you want to return a String?

    – GBlodgett
    Nov 21 '18 at 16:18











  • Yes - I want String

    – Peter Penzov
    Nov 21 '18 at 16:20






  • 1





    What have you tried? Where are you stuck?

    – Nicholas K
    Nov 21 '18 at 16:22
















-1















I have this number: 4200000000000000
I would like to leave only the first 4 digits and last 3 digits:



42000......000


Everything else should be replaced by dots. How I can implement this with some smart algorithm?










share|improve this question























  • Is the input a String? And I'm assuming you want to return a String?

    – GBlodgett
    Nov 21 '18 at 16:18











  • Yes - I want String

    – Peter Penzov
    Nov 21 '18 at 16:20






  • 1





    What have you tried? Where are you stuck?

    – Nicholas K
    Nov 21 '18 at 16:22














-1












-1








-1








I have this number: 4200000000000000
I would like to leave only the first 4 digits and last 3 digits:



42000......000


Everything else should be replaced by dots. How I can implement this with some smart algorithm?










share|improve this question














I have this number: 4200000000000000
I would like to leave only the first 4 digits and last 3 digits:



42000......000


Everything else should be replaced by dots. How I can implement this with some smart algorithm?







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 16:12









Peter PenzovPeter Penzov

14060184389




14060184389













  • Is the input a String? And I'm assuming you want to return a String?

    – GBlodgett
    Nov 21 '18 at 16:18











  • Yes - I want String

    – Peter Penzov
    Nov 21 '18 at 16:20






  • 1





    What have you tried? Where are you stuck?

    – Nicholas K
    Nov 21 '18 at 16:22



















  • Is the input a String? And I'm assuming you want to return a String?

    – GBlodgett
    Nov 21 '18 at 16:18











  • Yes - I want String

    – Peter Penzov
    Nov 21 '18 at 16:20






  • 1





    What have you tried? Where are you stuck?

    – Nicholas K
    Nov 21 '18 at 16:22

















Is the input a String? And I'm assuming you want to return a String?

– GBlodgett
Nov 21 '18 at 16:18





Is the input a String? And I'm assuming you want to return a String?

– GBlodgett
Nov 21 '18 at 16:18













Yes - I want String

– Peter Penzov
Nov 21 '18 at 16:20





Yes - I want String

– Peter Penzov
Nov 21 '18 at 16:20




1




1





What have you tried? Where are you stuck?

– Nicholas K
Nov 21 '18 at 16:22





What have you tried? Where are you stuck?

– Nicholas K
Nov 21 '18 at 16:22












6 Answers
6






active

oldest

votes


















1














Why not use a StringBuilder and the substring method:



public static String foo(long num) {
String numToString = String.valueOf(num);
return new StringBuilder()
.append(numToString.substring(0 , 4))
.append("....")
.append(numToString.substring(numToString.length()-3, numToString.length()))
.toString();
}


When inputted 4200000000000000 it outputs:



4200....000


Or if the input is already a String:



public static String foo(String str) {
return new StringBuilder()
.append(str.substring(0 , 4))
.append("....")
.append(str.substring(str.length()-3, str.length()))
.toString();
}





share|improve this answer

































    0














    Parse your number into a string and try this:



    int last = 3;
    int first = 4;
    String number = '4200000000000000';
    String start = number.substring(0,first-1);
    String end = number.substring(number.length()-last,number.length()-1);
    String dots = '';

    for(int i = 0; i<number.length()-last-first;i++){
    dots = dots + '.';
    }

    String result = start + dots + end;





    share|improve this answer































      0














      You can use something like this,



      public class Main {

      public static void main(String args) {
      System.out.println(convert("4200000000000000", 4, 3));
      }

      static String convert(String number, int firstDigits, int lastDigits) {
      String first = number.substring(0, firstDigits);
      String middle = number.substring(firstDigits, number.length() - lastDigits).replaceAll("0", ".");
      String last = number.substring(number.length() - lastDigits, number.length());

      return first + middle + last;
      }
      }





      share|improve this answer































        0














        You could convert it to a char array, alter it, then convert it back into a string



            char charArray = originalNumber.toCharArray();

        for (int i; i < charArray.length; i++) {
        if (i <= 4 || i >= charArray.length - 3) {
        charArray[i] = ".";
        }
        }
        String outputString = new String(charArray);





        share|improve this answer































          0














          This will replace all chars from the 4th char up to the 4th from the end with '.':



              String start = "4200000000000000";
          System.out.println(start);

          String target = start;
          if (start.length() > 7) {
          target = new StringBuilder()
          .append(start.substring(0, 4))
          .append(new String(new char[start.length() - 7]).replaceAll(".", "."))
          .append(start.substring(start.length() - 3))
          .toString();
          }
          System.out.println(target);


          will print



          4200000000000000
          4200.........000





          share|improve this answer































            0














            Using substring method of the String class :



            String str = "4200000000000000";
            String res = str.substring(0,4)+ str.substring(4,str.length()-3).replaceAll(".", ".") + str.substring(str.length()-3);


            If you are using Apache commons library, you can use repeat method to create masking string of specified length and the overlay method of StringUtils class to overlay part of the String :



            String str = "4200000000000000";
            String mask= StringUtils.repeat('.', str.length()-7);
            String res = StringUtils.overlay(str, mask, 4, str.length()-3);





            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',
              autoActivateHeartbeat: false,
              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%2f53416211%2freplace-fixed-number-of-symbols-in-string%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Why not use a StringBuilder and the substring method:



              public static String foo(long num) {
              String numToString = String.valueOf(num);
              return new StringBuilder()
              .append(numToString.substring(0 , 4))
              .append("....")
              .append(numToString.substring(numToString.length()-3, numToString.length()))
              .toString();
              }


              When inputted 4200000000000000 it outputs:



              4200....000


              Or if the input is already a String:



              public static String foo(String str) {
              return new StringBuilder()
              .append(str.substring(0 , 4))
              .append("....")
              .append(str.substring(str.length()-3, str.length()))
              .toString();
              }





              share|improve this answer






























                1














                Why not use a StringBuilder and the substring method:



                public static String foo(long num) {
                String numToString = String.valueOf(num);
                return new StringBuilder()
                .append(numToString.substring(0 , 4))
                .append("....")
                .append(numToString.substring(numToString.length()-3, numToString.length()))
                .toString();
                }


                When inputted 4200000000000000 it outputs:



                4200....000


                Or if the input is already a String:



                public static String foo(String str) {
                return new StringBuilder()
                .append(str.substring(0 , 4))
                .append("....")
                .append(str.substring(str.length()-3, str.length()))
                .toString();
                }





                share|improve this answer




























                  1












                  1








                  1







                  Why not use a StringBuilder and the substring method:



                  public static String foo(long num) {
                  String numToString = String.valueOf(num);
                  return new StringBuilder()
                  .append(numToString.substring(0 , 4))
                  .append("....")
                  .append(numToString.substring(numToString.length()-3, numToString.length()))
                  .toString();
                  }


                  When inputted 4200000000000000 it outputs:



                  4200....000


                  Or if the input is already a String:



                  public static String foo(String str) {
                  return new StringBuilder()
                  .append(str.substring(0 , 4))
                  .append("....")
                  .append(str.substring(str.length()-3, str.length()))
                  .toString();
                  }





                  share|improve this answer















                  Why not use a StringBuilder and the substring method:



                  public static String foo(long num) {
                  String numToString = String.valueOf(num);
                  return new StringBuilder()
                  .append(numToString.substring(0 , 4))
                  .append("....")
                  .append(numToString.substring(numToString.length()-3, numToString.length()))
                  .toString();
                  }


                  When inputted 4200000000000000 it outputs:



                  4200....000


                  Or if the input is already a String:



                  public static String foo(String str) {
                  return new StringBuilder()
                  .append(str.substring(0 , 4))
                  .append("....")
                  .append(str.substring(str.length()-3, str.length()))
                  .toString();
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '18 at 16:29

























                  answered Nov 21 '18 at 16:24









                  GBlodgettGBlodgett

                  10.2k42035




                  10.2k42035

























                      0














                      Parse your number into a string and try this:



                      int last = 3;
                      int first = 4;
                      String number = '4200000000000000';
                      String start = number.substring(0,first-1);
                      String end = number.substring(number.length()-last,number.length()-1);
                      String dots = '';

                      for(int i = 0; i<number.length()-last-first;i++){
                      dots = dots + '.';
                      }

                      String result = start + dots + end;





                      share|improve this answer




























                        0














                        Parse your number into a string and try this:



                        int last = 3;
                        int first = 4;
                        String number = '4200000000000000';
                        String start = number.substring(0,first-1);
                        String end = number.substring(number.length()-last,number.length()-1);
                        String dots = '';

                        for(int i = 0; i<number.length()-last-first;i++){
                        dots = dots + '.';
                        }

                        String result = start + dots + end;





                        share|improve this answer


























                          0












                          0








                          0







                          Parse your number into a string and try this:



                          int last = 3;
                          int first = 4;
                          String number = '4200000000000000';
                          String start = number.substring(0,first-1);
                          String end = number.substring(number.length()-last,number.length()-1);
                          String dots = '';

                          for(int i = 0; i<number.length()-last-first;i++){
                          dots = dots + '.';
                          }

                          String result = start + dots + end;





                          share|improve this answer













                          Parse your number into a string and try this:



                          int last = 3;
                          int first = 4;
                          String number = '4200000000000000';
                          String start = number.substring(0,first-1);
                          String end = number.substring(number.length()-last,number.length()-1);
                          String dots = '';

                          for(int i = 0; i<number.length()-last-first;i++){
                          dots = dots + '.';
                          }

                          String result = start + dots + end;






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 '18 at 16:27









                          SilvanSilvan

                          609




                          609























                              0














                              You can use something like this,



                              public class Main {

                              public static void main(String args) {
                              System.out.println(convert("4200000000000000", 4, 3));
                              }

                              static String convert(String number, int firstDigits, int lastDigits) {
                              String first = number.substring(0, firstDigits);
                              String middle = number.substring(firstDigits, number.length() - lastDigits).replaceAll("0", ".");
                              String last = number.substring(number.length() - lastDigits, number.length());

                              return first + middle + last;
                              }
                              }





                              share|improve this answer




























                                0














                                You can use something like this,



                                public class Main {

                                public static void main(String args) {
                                System.out.println(convert("4200000000000000", 4, 3));
                                }

                                static String convert(String number, int firstDigits, int lastDigits) {
                                String first = number.substring(0, firstDigits);
                                String middle = number.substring(firstDigits, number.length() - lastDigits).replaceAll("0", ".");
                                String last = number.substring(number.length() - lastDigits, number.length());

                                return first + middle + last;
                                }
                                }





                                share|improve this answer


























                                  0












                                  0








                                  0







                                  You can use something like this,



                                  public class Main {

                                  public static void main(String args) {
                                  System.out.println(convert("4200000000000000", 4, 3));
                                  }

                                  static String convert(String number, int firstDigits, int lastDigits) {
                                  String first = number.substring(0, firstDigits);
                                  String middle = number.substring(firstDigits, number.length() - lastDigits).replaceAll("0", ".");
                                  String last = number.substring(number.length() - lastDigits, number.length());

                                  return first + middle + last;
                                  }
                                  }





                                  share|improve this answer













                                  You can use something like this,



                                  public class Main {

                                  public static void main(String args) {
                                  System.out.println(convert("4200000000000000", 4, 3));
                                  }

                                  static String convert(String number, int firstDigits, int lastDigits) {
                                  String first = number.substring(0, firstDigits);
                                  String middle = number.substring(firstDigits, number.length() - lastDigits).replaceAll("0", ".");
                                  String last = number.substring(number.length() - lastDigits, number.length());

                                  return first + middle + last;
                                  }
                                  }






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 21 '18 at 16:27









                                  SandSand

                                  1,7182721




                                  1,7182721























                                      0














                                      You could convert it to a char array, alter it, then convert it back into a string



                                          char charArray = originalNumber.toCharArray();

                                      for (int i; i < charArray.length; i++) {
                                      if (i <= 4 || i >= charArray.length - 3) {
                                      charArray[i] = ".";
                                      }
                                      }
                                      String outputString = new String(charArray);





                                      share|improve this answer




























                                        0














                                        You could convert it to a char array, alter it, then convert it back into a string



                                            char charArray = originalNumber.toCharArray();

                                        for (int i; i < charArray.length; i++) {
                                        if (i <= 4 || i >= charArray.length - 3) {
                                        charArray[i] = ".";
                                        }
                                        }
                                        String outputString = new String(charArray);





                                        share|improve this answer


























                                          0












                                          0








                                          0







                                          You could convert it to a char array, alter it, then convert it back into a string



                                              char charArray = originalNumber.toCharArray();

                                          for (int i; i < charArray.length; i++) {
                                          if (i <= 4 || i >= charArray.length - 3) {
                                          charArray[i] = ".";
                                          }
                                          }
                                          String outputString = new String(charArray);





                                          share|improve this answer













                                          You could convert it to a char array, alter it, then convert it back into a string



                                              char charArray = originalNumber.toCharArray();

                                          for (int i; i < charArray.length; i++) {
                                          if (i <= 4 || i >= charArray.length - 3) {
                                          charArray[i] = ".";
                                          }
                                          }
                                          String outputString = new String(charArray);






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 21 '18 at 16:28









                                          AleksWAleksW

                                          1239




                                          1239























                                              0














                                              This will replace all chars from the 4th char up to the 4th from the end with '.':



                                                  String start = "4200000000000000";
                                              System.out.println(start);

                                              String target = start;
                                              if (start.length() > 7) {
                                              target = new StringBuilder()
                                              .append(start.substring(0, 4))
                                              .append(new String(new char[start.length() - 7]).replaceAll(".", "."))
                                              .append(start.substring(start.length() - 3))
                                              .toString();
                                              }
                                              System.out.println(target);


                                              will print



                                              4200000000000000
                                              4200.........000





                                              share|improve this answer




























                                                0














                                                This will replace all chars from the 4th char up to the 4th from the end with '.':



                                                    String start = "4200000000000000";
                                                System.out.println(start);

                                                String target = start;
                                                if (start.length() > 7) {
                                                target = new StringBuilder()
                                                .append(start.substring(0, 4))
                                                .append(new String(new char[start.length() - 7]).replaceAll(".", "."))
                                                .append(start.substring(start.length() - 3))
                                                .toString();
                                                }
                                                System.out.println(target);


                                                will print



                                                4200000000000000
                                                4200.........000





                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  This will replace all chars from the 4th char up to the 4th from the end with '.':



                                                      String start = "4200000000000000";
                                                  System.out.println(start);

                                                  String target = start;
                                                  if (start.length() > 7) {
                                                  target = new StringBuilder()
                                                  .append(start.substring(0, 4))
                                                  .append(new String(new char[start.length() - 7]).replaceAll(".", "."))
                                                  .append(start.substring(start.length() - 3))
                                                  .toString();
                                                  }
                                                  System.out.println(target);


                                                  will print



                                                  4200000000000000
                                                  4200.........000





                                                  share|improve this answer













                                                  This will replace all chars from the 4th char up to the 4th from the end with '.':



                                                      String start = "4200000000000000";
                                                  System.out.println(start);

                                                  String target = start;
                                                  if (start.length() > 7) {
                                                  target = new StringBuilder()
                                                  .append(start.substring(0, 4))
                                                  .append(new String(new char[start.length() - 7]).replaceAll(".", "."))
                                                  .append(start.substring(start.length() - 3))
                                                  .toString();
                                                  }
                                                  System.out.println(target);


                                                  will print



                                                  4200000000000000
                                                  4200.........000






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 21 '18 at 16:50









                                                  forpasforpas

                                                  13.1k3524




                                                  13.1k3524























                                                      0














                                                      Using substring method of the String class :



                                                      String str = "4200000000000000";
                                                      String res = str.substring(0,4)+ str.substring(4,str.length()-3).replaceAll(".", ".") + str.substring(str.length()-3);


                                                      If you are using Apache commons library, you can use repeat method to create masking string of specified length and the overlay method of StringUtils class to overlay part of the String :



                                                      String str = "4200000000000000";
                                                      String mask= StringUtils.repeat('.', str.length()-7);
                                                      String res = StringUtils.overlay(str, mask, 4, str.length()-3);





                                                      share|improve this answer




























                                                        0














                                                        Using substring method of the String class :



                                                        String str = "4200000000000000";
                                                        String res = str.substring(0,4)+ str.substring(4,str.length()-3).replaceAll(".", ".") + str.substring(str.length()-3);


                                                        If you are using Apache commons library, you can use repeat method to create masking string of specified length and the overlay method of StringUtils class to overlay part of the String :



                                                        String str = "4200000000000000";
                                                        String mask= StringUtils.repeat('.', str.length()-7);
                                                        String res = StringUtils.overlay(str, mask, 4, str.length()-3);





                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          Using substring method of the String class :



                                                          String str = "4200000000000000";
                                                          String res = str.substring(0,4)+ str.substring(4,str.length()-3).replaceAll(".", ".") + str.substring(str.length()-3);


                                                          If you are using Apache commons library, you can use repeat method to create masking string of specified length and the overlay method of StringUtils class to overlay part of the String :



                                                          String str = "4200000000000000";
                                                          String mask= StringUtils.repeat('.', str.length()-7);
                                                          String res = StringUtils.overlay(str, mask, 4, str.length()-3);





                                                          share|improve this answer













                                                          Using substring method of the String class :



                                                          String str = "4200000000000000";
                                                          String res = str.substring(0,4)+ str.substring(4,str.length()-3).replaceAll(".", ".") + str.substring(str.length()-3);


                                                          If you are using Apache commons library, you can use repeat method to create masking string of specified length and the overlay method of StringUtils class to overlay part of the String :



                                                          String str = "4200000000000000";
                                                          String mask= StringUtils.repeat('.', str.length()-7);
                                                          String res = StringUtils.overlay(str, mask, 4, str.length()-3);






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 21 '18 at 16:53









                                                          EritreanEritrean

                                                          3,4321914




                                                          3,4321914






























                                                              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.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function () {
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416211%2freplace-fixed-number-of-symbols-in-string%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

                                                              MongoDB - Not Authorized To Execute Command

                                                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                                              Npm cannot find a required file even through it is in the searched directory