Format a string into a specific format












1















Currently i have a 9 String number



String s = 123456789


how can i convert this string into this kind of format?



String newS = 12 - 34 - 5678 - 9









share|improve this question




















  • 4





    which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?

    – Vladyslav Matviienko
    Nov 13 '18 at 7:17






  • 1





    are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.

    – Gautham M
    Nov 13 '18 at 7:49
















1















Currently i have a 9 String number



String s = 123456789


how can i convert this string into this kind of format?



String newS = 12 - 34 - 5678 - 9









share|improve this question




















  • 4





    which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?

    – Vladyslav Matviienko
    Nov 13 '18 at 7:17






  • 1





    are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.

    – Gautham M
    Nov 13 '18 at 7:49














1












1








1


1






Currently i have a 9 String number



String s = 123456789


how can i convert this string into this kind of format?



String newS = 12 - 34 - 5678 - 9









share|improve this question
















Currently i have a 9 String number



String s = 123456789


how can i convert this string into this kind of format?



String newS = 12 - 34 - 5678 - 9






java string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 7:38









Hayk Abelyan

216314




216314










asked Nov 13 '18 at 7:13









netflix spotifynetflix spotify

587




587








  • 4





    which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?

    – Vladyslav Matviienko
    Nov 13 '18 at 7:17






  • 1





    are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.

    – Gautham M
    Nov 13 '18 at 7:49














  • 4





    which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?

    – Vladyslav Matviienko
    Nov 13 '18 at 7:17






  • 1





    are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.

    – Gautham M
    Nov 13 '18 at 7:49








4




4





which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?

– Vladyslav Matviienko
Nov 13 '18 at 7:17





which is an algorithm you want to use? What do you want the output format to be? 2 - 2 - 4 - 1 characters?

– Vladyslav Matviienko
Nov 13 '18 at 7:17




1




1





are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.

– Gautham M
Nov 13 '18 at 7:49





are you considering strings of size 9 only? Otherwise please mention the format in which such strings should be displayed.

– Gautham M
Nov 13 '18 at 7:49












4 Answers
4






active

oldest

votes


















1














Try this one :



  public static String getFormattedAccountNumber(String result){
StringBuilder stringBuilder;
if (result.length() > 3 && result.length() < 13) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(3, "-");
result = stringBuilder.toString();
if (result.length() > 6 && result.length() < 14) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(6, "-");
result = stringBuilder.toString();
if (result.length() > 13 && result.length() < 15) {
stringBuilder = new StringBuilder(result);
stringBuilder.insert(13, "-");
result = stringBuilder.toString();
}
}
}
return result;
}





share|improve this answer































    4














    I hope this will help you.



    public static String formatString(String str) {
    return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
    }





    share|improve this answer

































      0














      With Java8 streams you can get a cleaner version...



      String s = "123456789";

      AtomicInteger pos = new AtomicInteger();
      String newS = IntStream.of(2, 2, 4, 1)
      .mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
      .collect(Collectors.joining(" - "));





      share|improve this answer
























      • notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...

        – rifaqat
        Nov 13 '18 at 8:14



















      0














      You can use a regular expression:



          String s = "123456789";
      Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
      Matcher matcher = pattern.matcher(s);
      String formatted = matcher.find() ?
      matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
      "";
      System.out.println(formatted); // 12 - 34 - 5678 - 9





      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%2f53275687%2fformat-a-string-into-a-specific-format%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Try this one :



          public static String getFormattedAccountNumber(String result){
        StringBuilder stringBuilder;
        if (result.length() > 3 && result.length() < 13) {
        stringBuilder = new StringBuilder(result);
        stringBuilder.insert(3, "-");
        result = stringBuilder.toString();
        if (result.length() > 6 && result.length() < 14) {
        stringBuilder = new StringBuilder(result);
        stringBuilder.insert(6, "-");
        result = stringBuilder.toString();
        if (result.length() > 13 && result.length() < 15) {
        stringBuilder = new StringBuilder(result);
        stringBuilder.insert(13, "-");
        result = stringBuilder.toString();
        }
        }
        }
        return result;
        }





        share|improve this answer




























          1














          Try this one :



            public static String getFormattedAccountNumber(String result){
          StringBuilder stringBuilder;
          if (result.length() > 3 && result.length() < 13) {
          stringBuilder = new StringBuilder(result);
          stringBuilder.insert(3, "-");
          result = stringBuilder.toString();
          if (result.length() > 6 && result.length() < 14) {
          stringBuilder = new StringBuilder(result);
          stringBuilder.insert(6, "-");
          result = stringBuilder.toString();
          if (result.length() > 13 && result.length() < 15) {
          stringBuilder = new StringBuilder(result);
          stringBuilder.insert(13, "-");
          result = stringBuilder.toString();
          }
          }
          }
          return result;
          }





          share|improve this answer


























            1












            1








            1







            Try this one :



              public static String getFormattedAccountNumber(String result){
            StringBuilder stringBuilder;
            if (result.length() > 3 && result.length() < 13) {
            stringBuilder = new StringBuilder(result);
            stringBuilder.insert(3, "-");
            result = stringBuilder.toString();
            if (result.length() > 6 && result.length() < 14) {
            stringBuilder = new StringBuilder(result);
            stringBuilder.insert(6, "-");
            result = stringBuilder.toString();
            if (result.length() > 13 && result.length() < 15) {
            stringBuilder = new StringBuilder(result);
            stringBuilder.insert(13, "-");
            result = stringBuilder.toString();
            }
            }
            }
            return result;
            }





            share|improve this answer













            Try this one :



              public static String getFormattedAccountNumber(String result){
            StringBuilder stringBuilder;
            if (result.length() > 3 && result.length() < 13) {
            stringBuilder = new StringBuilder(result);
            stringBuilder.insert(3, "-");
            result = stringBuilder.toString();
            if (result.length() > 6 && result.length() < 14) {
            stringBuilder = new StringBuilder(result);
            stringBuilder.insert(6, "-");
            result = stringBuilder.toString();
            if (result.length() > 13 && result.length() < 15) {
            stringBuilder = new StringBuilder(result);
            stringBuilder.insert(13, "-");
            result = stringBuilder.toString();
            }
            }
            }
            return result;
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 7:22









            FreelancsAndroidLovesyouFreelancsAndroidLovesyou

            159112




            159112

























                4














                I hope this will help you.



                public static String formatString(String str) {
                return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
                }





                share|improve this answer






























                  4














                  I hope this will help you.



                  public static String formatString(String str) {
                  return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
                  }





                  share|improve this answer




























                    4












                    4








                    4







                    I hope this will help you.



                    public static String formatString(String str) {
                    return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
                    }





                    share|improve this answer















                    I hope this will help you.



                    public static String formatString(String str) {
                    return str.substring(0, 1) + " - " + str.substring(2, 3) + " - " + str.substring(4, 7) + " - " + str.substring(8);
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 13 '18 at 7:57









                    oleg.cherednik

                    6,39621118




                    6,39621118










                    answered Nov 13 '18 at 7:50









                    macmurimacmuri

                    644




                    644























                        0














                        With Java8 streams you can get a cleaner version...



                        String s = "123456789";

                        AtomicInteger pos = new AtomicInteger();
                        String newS = IntStream.of(2, 2, 4, 1)
                        .mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
                        .collect(Collectors.joining(" - "));





                        share|improve this answer
























                        • notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...

                          – rifaqat
                          Nov 13 '18 at 8:14
















                        0














                        With Java8 streams you can get a cleaner version...



                        String s = "123456789";

                        AtomicInteger pos = new AtomicInteger();
                        String newS = IntStream.of(2, 2, 4, 1)
                        .mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
                        .collect(Collectors.joining(" - "));





                        share|improve this answer
























                        • notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...

                          – rifaqat
                          Nov 13 '18 at 8:14














                        0












                        0








                        0







                        With Java8 streams you can get a cleaner version...



                        String s = "123456789";

                        AtomicInteger pos = new AtomicInteger();
                        String newS = IntStream.of(2, 2, 4, 1)
                        .mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
                        .collect(Collectors.joining(" - "));





                        share|improve this answer













                        With Java8 streams you can get a cleaner version...



                        String s = "123456789";

                        AtomicInteger pos = new AtomicInteger();
                        String newS = IntStream.of(2, 2, 4, 1)
                        .mapToObj(n -> s.substring(pos.getAndAdd(n), pos.get()))
                        .collect(Collectors.joining(" - "));






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 13 '18 at 8:12









                        rifaqatrifaqat

                        14617




                        14617













                        • notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...

                          – rifaqat
                          Nov 13 '18 at 8:14



















                        • notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...

                          – rifaqat
                          Nov 13 '18 at 8:14

















                        notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...

                        – rifaqat
                        Nov 13 '18 at 8:14





                        notice IntStream.of(2, 2, 4, 1) -- and joining(" - ") . ... so easily customisable without extra effort...

                        – rifaqat
                        Nov 13 '18 at 8:14











                        0














                        You can use a regular expression:



                            String s = "123456789";
                        Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
                        Matcher matcher = pattern.matcher(s);
                        String formatted = matcher.find() ?
                        matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
                        "";
                        System.out.println(formatted); // 12 - 34 - 5678 - 9





                        share|improve this answer




























                          0














                          You can use a regular expression:



                              String s = "123456789";
                          Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
                          Matcher matcher = pattern.matcher(s);
                          String formatted = matcher.find() ?
                          matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
                          "";
                          System.out.println(formatted); // 12 - 34 - 5678 - 9





                          share|improve this answer


























                            0












                            0








                            0







                            You can use a regular expression:



                                String s = "123456789";
                            Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
                            Matcher matcher = pattern.matcher(s);
                            String formatted = matcher.find() ?
                            matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
                            "";
                            System.out.println(formatted); // 12 - 34 - 5678 - 9





                            share|improve this answer













                            You can use a regular expression:



                                String s = "123456789";
                            Pattern pattern = Pattern.compile("(\d{2})(\d{2})(\d{4})(\d)");
                            Matcher matcher = pattern.matcher(s);
                            String formatted = matcher.find() ?
                            matcher.group(1) + " - " + matcher.group(2) + " - " + matcher.group(3) + " - " + matcher.group(4) :
                            "";
                            System.out.println(formatted); // 12 - 34 - 5678 - 9






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 8:16









                            LuCioLuCio

                            2,7641823




                            2,7641823






























                                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%2f53275687%2fformat-a-string-into-a-specific-format%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

                                Coverage of Google Street View

                                Full-time equivalent

                                Surfing