How to translate “Lorem 3 ipsum dolor sit amet” into SEO friendly “Lorem-3-ipsum-dolor-sit-amet” in Java?

后端 未结 3 510
广开言路
广开言路 2021-01-23 10:49

In my blog app, a user can enter any text as a title for their entry and then I generate a URL based on the text.

I validate their title to make sure it only co

3条回答
  •  时光取名叫无心
    2021-01-23 11:23

    Since it won't seem to allow me to comment. I would do:

    String s = "Lorem 3 ipsum dolor sit amet"
    s = s.replaceAll(" ","_");
    

    Using the Underscore character instead because it is a space indicator. Its been a while since I've done java but I know there is a function in .Net that will cleanup a file name so its safe for the file system. I lot of the same general rules applies to a URL so if you can find one in the API it be worth taking a look.

提交回复
热议问题