From bbea62a5c794a354c49d3546dee0056437454960 Mon Sep 17 00:00:00 2001 From: Carpenter Lee Date: Fri, 19 Oct 2018 11:04:30 +0800 Subject: [PATCH] Add StringUtil.capitalize() method --- .../alibaba/csp/sentinel/util/StringUtil.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/StringUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/StringUtil.java index a355ef56..9fb76247 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/StringUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/StringUtil.java @@ -117,4 +117,22 @@ public final class StringUtil { return true; } + + public static String capitalize(String str) { + return changeFirstCharacterCase(str, true); + } + + private static String changeFirstCharacterCase(String str, boolean capitalize) { + if (str == null || str.length() == 0) { + return str; + } + StringBuilder buf = new StringBuilder(str.length()); + if (capitalize) { + buf.append(Character.toUpperCase(str.charAt(0))); + } else { + buf.append(Character.toLowerCase(str.charAt(0))); + } + buf.append(str.substring(1)); + return buf.toString(); + } }