Printing a 3-in-3 formatted sequence of numbers separated by dash

Asked

Viewed 98 times

1

Let’s say I have the following entry String S = "00-44 48 5555 8361" and that I need to return this character string divided 3 by 3, separated by "-", as follows:

Output: 004-448-555-583-61

The amount of characters may vary. Below the following scope:

public class Solution{
    public static String solution(String S){
    }
}

public static void main(String[] args) {    
        String S = "00-44  48 5555 8361";   
        System.out.print(solution(S));

    }

}

In an effective way, how to solve this in Java 8?

1 answer

2


Regex does magic:

class Solution {
    public static void main(String[] args) {
        String s = "00-44  48 5555 8361";
        System.out.println(solution(s));
        String s2 = "00-44  48 5555 8361 xxx 1";
        System.out.println(solution(s2));
        String s3 = "12345";
        System.out.println(solution(s3));
    }

    public static String solution(String s) {
        return s.replaceAll("[^\\d]", "").replaceAll("...", "$0-").replaceAll("-$", "");
    }
}

Here’s the way out:

004-448-555-583-61
004-448-555-583-611
123-45

See here working on ideone.

Explanation:

  • The replaceAll is a method that operates on three strings: The instance over which the method is called (A), the first parameter (B) and the second parameter (C). This method searches for occurrences of the regular expression of B within the string A and replaces them with C, returning the result after all substitutions have been made. References denoted with $ can be used in C to reference groups defined in B.

  • The replaceAll("[^\\d]", "") uses regular expression [^\\d]. This regular expression is meant to represent anything other than a number. The excerpt \\d is what represents a number. The [^ ... ] indicates all characters except those between the [^ and the ]. Soon, [^\\d] represents all characters except numerical ones. Thus, this will replace any character other than a number with the empty string. That is, it serves to boot all non-numeric characters from the string.

  • The replaceAll("...", "$0-") uses regular expression .... The . represents any character, therefore ... are three characters. As the previous regular expression has already eliminated non-numeric ones, so this hence finds the digits in groups of three on three. Therefore, this expression replaces each trio of numerical characters by what is represented by $0-. It turns out that $0 represents everything the regular expression found, which in this case is the three characters found. Therefore, this will replace each three characters by these same characters followed by a -.

  • It may be that about a - in the end, as in the case of "00-44 48 5555 8361 xxx 1". That’s why we have the replaceAll("-$", ""). The regular expression -$ means a - followed by the end of the string (which is represented by $). This means that the dash at the end of the string (if any) will be replaced by an empty string. That is, deleted.

  • Solved, your answer was amazing!

  • This Redux does magic, look at the amount of code lines! Very efficient your solution. Thank you very much!

  • @ssamaylns If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.