Verify if String matches a format String

前端 未结 7 1771
旧巷少年郎
旧巷少年郎 2021-02-13 09:15

In Java, how can you determine if a String matches a format string (ie: song%03d.mp3)?

In other words, how would you implement the following function?

7条回答
  •  孤城傲影
    2021-02-13 09:33

    the string class has the matches method, you can pass a regex there. String.matches(String)

    for the regex you can see this: http://download.oracle.com/javase/1,5.0/docs/api/java/util/regex/Pattern.html
    examples:

    "song001.mp3".matches("song\\d{3}\\.mp3");
    

提交回复
热议问题