java regex for UUID

后端 未结 2 1269
半阙折子戏
半阙折子戏 2021-01-17 20:34

I want to parse a String which has UUID in the below format

\"<urn:uuid:4324e9d5-8d1f-442c-96a4-6146640da7ce>\"

I have tried

2条回答
  •  忘掉有多难
    2021-01-17 21:04

    If this format don't be changed. I think more fast way is use String.substring() method. Example:

    String val = "<urn:uuid:4324e9d5-8d1f-442c-96a4-6146640da7ce>";
    String sUuid = val.substring(13, 49);
    UUID uuid =  UUID.fromString(sUuid);
    

    Inside class String used char array for store data, in package java.lang.String:

    public final class String
        implements java.io.Serializable, Comparable, CharSequence {
    ...
    113: /** The value is used for character storage. */
    114: private final char value[];
    ...
    }
    

    Method 'String substring(int beginIndex, int endIndex)' make the copy of array elements, from start to end index, and create new String on basis new array. Copying of array it is a very fast operation.

提交回复
热议问题