Java: Try implementing your own comma-separated formatter

At the beginning

See Easy way to put a comma every 3 digits such as price notation "If you write your own comma-separated logic without using String # format or NumberFormat, how would you implement it?" I was interested in that, so I tried to implement it easily

Implementation policy

I feel like I can get a subset of 3 digits with a regular expression and fold it with reduce. ~~ If you cut it out with tsubstring, the operation of subscripts seems to be troublesome ~~ If the number of characters is not a multiple of 3, it seems that you should forcibly enter the previous zero.

I tried to implement

MyFormatter.java


package formatter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyFormatter {
    public static void main(String[] args) throws Exception{
    	System.out.println(format("11"));
    	System.out.println(format("111"));
    	System.out.println(format("1111"));
    	System.out.println(format("11111"));
    	System.out.println(format("111111"));
    }	
    public static String format(String str) {
        Matcher matcher = Pattern.compile("(\\d{3})").matcher(makeStr(str));
        List<String> strs =new ArrayList<String>();
        //Remove the previous 0
        if(matcher.find()) {
        	strs.add(Integer.parseInt(matcher.group()) + "");
        }
        while(matcher.find()) {
        	strs.add(matcher.group());
        }
        //,It is cleaner to use reduce to add
    	return strs
    			.stream()
    			.reduce((s,s1)-> s+","+s1)
    			.get();
    	
    }
    public static String makeStr(String str) {
    	if(str.length()%3 ==2)return "0" + str;
    	else if(str.length()%3 ==1)return "00" + str;
    	else return str;
    }
}

Execution result

11
111
1,111
11,111
111,111

Impressions

--Java is troublesome to get a substring with a regular expression, I wish the group function returned a stream ――This logic seems to be applicable when you want to cut out a character string by XX characters.

Recommended Posts

Java: Try implementing your own comma-separated formatter
Create your own Java annotations
Try implementing Android Hilt in Java
Try implementing GraphQL server in Java
Make your own persistence FW (Java)
Handle your own annotations in Java
Try to organize your own object orientation
Understand java interface in your own way
Try Java 8 Stream
Create your own Android app for Java learning
Roughly try Java 9
[Java] Sort ArrayList with elements of your own class
Try implementing the Eratosthenes sieve using the Java standard library
It's late! Try implementing Android Notification in Java (Beginner)
Make your own pomodoro
Try Java return value
How to read your own YAML file (*****. Yml) in Java
It's late! Try implementing Android Work Manager in Java (Beginner)
Try implementing signature verification for elliptic curve cryptography in Java
Make your own simple server in Java and understand HTTP
Java: Start WAS with Docker and deploy your own application