Best way to create SEO friendly URI string

前端 未结 3 1182
-上瘾入骨i
-上瘾入骨i 2021-02-14 18:55

The method should allows only "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" chars in URI strings.

What is the best way to make

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 19:15

    The following regex will do the same thing as your algorithm. I'm not aware of libraries for doing this type of thing.

    String s = input
    .replaceAll(" ?- ?","-") // remove spaces around hyphens
    .replaceAll("[ ']","-") // turn spaces and quotes into hyphens
    .replaceAll("[^0-9a-zA-Z-]",""); // remove everything not in our allowed char set
    

提交回复
热议问题