3

I have a requirement like this. My code generates random strings and it can be alpha, numeric and alphanumeric ones.

Lets say one of the numeric strings are "7882347812". I want to format this to 788.234.7812 based on a pattern like 3chars.3chars.4chars

If its an alphanumeric one like "h34jh8we7k". Then format this to h3/4jh8/we7k based on a pattern like 2chars/4chars/4chars.

If its an alpha one like "jkythjyv". Then format this to jky$thj$yv based on a pattern like 3chars$3chars$2chars.

In general, the generated strings can contain chars [a-zA-Z0-9]. This should be formatted as I mentioned above with any of the special characters. The input should be the string & the formatter and output should be the formatted string. Even a custom formatter is also fine.

I know how to write code for this. Is there any standard way of doing this in Java?

Ani
  • 722
  • 9
  • 27
  • I assume you're trying to format phone numbers. There are several answers to this question already. [Here](http://stackoverflow.com/questions/7730006/java-format-a-string-as-a-telephone-number), [here](http://stackoverflow.com/questions/487906/java-phone-number-format-api), and [here](http://stackoverflow.com/questions/8196771/format-a-string-using-regex-in-java) – Cory Klein Oct 01 '14 at 16:54
  • No I am not. It can be any strings. – Ani Oct 01 '14 at 17:37
  • I got a solution for this and updated the answer. – Ani Oct 01 '14 at 20:27

3 Answers3

8

I got a solution for this:

MaskFormatter formatter = new MaskFormatter("A-AAAA-AAAA-A");
formatter.setValueContainsLiteralCharacters(false);
System.out.println(formatter.valueToString("1222233334"));

The output would be 1-2222-3333-4

Check http://docs.oracle.com/javase/7/docs/api/javax/swing/text/MaskFormatter.html for more details

Ani
  • 722
  • 9
  • 27
7

You could do this through replaceAll function.

System.out.println("7882347812".replaceAll("^(\\d{3})(\\d{3})(\\d{4})$", "$1.$2.$3"));

Output:

788.234.7812

OR

System.out.println("foo bar 7882347812".replaceAll("\\b(\\d{3})(\\d{3})(\\d{4})\\b", "$1.$2.$3"));

Output:

foo bar 788.234.7812
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • This implies the whole string is a 10-digit string, while in his post he said "My code generates random strings and it can be alpha, numeric and alphanumeric ones", which means it _could_ result in strings +/-10 characters, with more than just numbers. Your example could probably update to account for that and answer the question more completely (though I do note the ambiguity of the request, so there's that...) – Ryan J Oct 01 '14 at 17:36
  • then it would be `\\b(\\d{3})(\\d{3})(\\d{4})\\b` – Avinash Raj Oct 01 '14 at 17:37
0
^(.{3})(.{3})(.{4})$

Try this.This will work.See demo.

http://regex101.com/r/xP1dE9/1

vks
  • 67,027
  • 10
  • 91
  • 124
  • Whoa! regex101 is neat! As a side note, that might not work for the OP because it also accepts alpha characters. – Cory Klein Oct 01 '14 at 16:55
  • @CoryKlein OP says random string generated can be alpha numeric etc. – vks Oct 01 '14 at 16:56
  • Yea, it's ambiguous. His example gave a strictly numeric string, so I was assuming he only wanted to apply the pattern to numeric strings. (*shrug*) Whatever. – Cory Klein Oct 01 '14 at 16:58
  • 1
    @Ani My answer still holds. No, there is not a standard way of doing this in Java. There are ways that work and ways that don't. There are elegant ways, and ugly ways. – Cory Klein Oct 01 '14 at 18:00