Creating a UUID from a string with no dashes

前端 未结 10 2255
臣服心动
臣服心动 2020-11-29 01:15

How would I create a java.util.UUID from a string with no dashes?

\"5231b533ba17478798a3f2df37de2aD7\" => #uuid \"5231b533-ba17-4787-98a3-f2df37de2aD7\"
<         


        
10条回答
  •  鱼传尺愫
    2020-11-29 01:55

    You could do a goofy regular expression replacement:

    String digits = "5231b533ba17478798a3f2df37de2aD7";                         
    String uuid = digits.replaceAll(                                            
        "(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",                            
        "$1-$2-$3-$4-$5");                                                      
    System.out.println(uuid); // => 5231b533-ba17-4787-98a3-f2df37de2aD7
    

提交回复
热议问题