patternjavaMajor
Appending a trailing slash if needed
Viewed 0 times
trailingneededslashappending
Problem
I like to overcomplicate things, this is fairly simple though but I would like to keep it simpler without lowering readability. Not just to improve my code but also my self.
private String checkTrailingSlash(String website) {
if(!website.endsWith("/")){
website = website + "/";
}
return website;
}Solution
You could use a ternary operator to inline the condition into a single
return statement:private String checkTrailingSlash(String website) {
return website.endsWith("/") ? website : website + "/";
}Code Snippets
private String checkTrailingSlash(String website) {
return website.endsWith("/") ? website : website + "/";
}Context
StackExchange Code Review Q#139596, answer score: 23
Revisions (0)
No revisions yet.