Last active
December 29, 2017 09:50
-
-
Save lamngockhuong/101c260de1d43612805fb7c34bd2e074 to your computer and use it in GitHub Desktop.
[Convert Text to Slug String in web] #java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Slug { | |
public String createSlug(String title) { | |
String slug = Normalizer.normalize(title, Normalizer.Form.NFD); | |
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+"); | |
slug = pattern.matcher(slug).replaceAll(""); | |
slug = slug.toLowerCase(); | |
// Thay đ thành d | |
slug = slug.replaceAll("đ", "d"); | |
// Xóa các ký tự đặt biệt | |
slug = slug.replaceAll("([^0-9a-z-\\s])", ""); | |
// Thay space thành dấu gạch ngang | |
slug = slug.replaceAll("[\\s]", "-"); | |
// Đổi nhiều ký tự gạch ngang liên tiếp thành 1 ký tự gạch ngang | |
slug = slug.replaceAll("(-+)", "-"); | |
// Xóa các ký tự gạch ngang ở đầu và cuối | |
slug = slug.replaceAll("^-+", ""); | |
slug = slug.replaceAll("-+$", ""); | |
return slug; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment