One would think this life would be easier than it currently is. I mean come on wouldn’t ou think that? Of course you would. So why can’t we each believe in it I for one would want to believe in it. It would give me something to do with my day at least. But well there’s something else on my mind. We have to be able to understand what it is exactly, if we don’t? Then that’s something quite different now isn’t it? Yeah that’s what I was thinking.
Sometimes you want a slug to make a filename. A slug is a group of words where the punctuation is removed and spaces are replaced with hyphens.
Here is a simple way to implement this in Java. (It’s met my needs so far.)
/**
* Returns a phrase into a slug ie: This Is Good (this-is-good.md)
* @param input The text to transform into a slug
* @return The slug of the text passed in
*/
public static String toSlug(String input) {
return input
.toLowerCase()
.replaceAll("[^a-z0-9\\s]", "") // remove punctuation
.trim()
.replaceAll("\\s+", "-") // spaces → hyphens
+ ".md";
}
It can use some work. I have noticed if you have a hyphen in the word you pass in, it will put – which you might not want. So yeah some tweaks can be added to it for sure.
Comments
Post a Comment