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?

3
  • I assume you're trying to format phone numbers. There are several answers to this question already. Here, here, and here Commented Oct 1, 2014 at 16:54
  • No I am not. It can be any strings. Commented Oct 1, 2014 at 17:37
  • I got a solution for this and updated the answer. Commented Oct 1, 2014 at 20:27

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

That is yet another creative solution! Good job.
Not applicable for Android
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

2 Comments

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...)
then it would be \\b(\\d{3})(\\d{3})(\\d{4})\\b
0
^(.{3})(.{3})(.{4})$

Try this.This will work.See demo.

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

4 Comments

Whoa! regex101 is neat! As a side note, that might not work for the OP because it also accepts alpha characters.
@CoryKlein OP says random string generated can be alpha numeric etc.
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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.