Class CaseBreaker
break
and convert
input strings (normally
identifier strings) in camelCase
, UpperCamelCase
, snake_case
,
UPPER_SNAKE_CASE
and dash-case
etc.
Unlike CaseFormat
, this class doesn't require you to know the input casing. You can
take any string and then extract or convert into the target casing.
Warning: This class doesn't recognize supplementary code points.
- Since:
- 6.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionbreakCase
(CharSequence text) Returns a lazy stream of words split out fromtext
, delimited by non-letter-digit ascii characters, and further split atlowerCamelCase
andUpperCamelCase
boundaries.static String
Convertsinput
string to using the givenCaseFormat
.withLowerCaseChars
(com.google.common.base.CharMatcher camelLower) Returns a new instance usingcamelLower
to identify lower case characters (don't forget to include digits if they should also be treated as lower case).withPunctuationChars
(com.google.common.base.CharMatcher punctuation) Returns a new instance usingpunctuation
to identify punctuation characters (ones that separate words but aren't themselves included in the result), for example if you want to support dash-case using the en dash (–) character.
-
Constructor Details
-
CaseBreaker
public CaseBreaker()
-
-
Method Details
-
withPunctuationChars
Returns a new instance usingpunctuation
to identify punctuation characters (ones that separate words but aren't themselves included in the result), for example if you want to support dash-case using the en dash (–) character.- Since:
- 7.0
-
withLowerCaseChars
Returns a new instance usingcamelLower
to identify lower case characters (don't forget to include digits if they should also be treated as lower case). -
breakCase
Returns a lazy stream of words split out fromtext
, delimited by non-letter-digit ascii characters, and further split atlowerCamelCase
andUpperCamelCase
boundaries.Examples:
breakCase("userId") => ["user", "Id"] breakCase("field_name") => ["field", "name"] breakCase("CONSTANT_NAME") => ["CONSTANT", "NAME"] breakCase("dash-case") => ["dash", "case"] breakCase("3 separate words") => ["3", "separate", "words"] breakCase("TheURLs") => ["The", "URLs"] breakCase("🅣ⓗⓔ🅤🅡🅛ⓢ") => ["🅣ⓗⓔ", "🅤🅡🅛ⓢ""] breakCase("UpgradeIPv4ToIPv6") => ["Upgrade", "IPv4", "To", "IPv6"]
By default, non-alphanumeric ascii characters are treated as case delimiter characters. And Java lower case characters and ascii digits are considered to be lower case when breaking up camel case.
Besides used as case delimiters, non-letter-digit ascii characters are filtered out from the returned words.
If the default setting doesn't work for you, it can be customized by using
invalid reference
#withCaseDelimiterChars
withLowerCaseChars(com.google.common.base.CharMatcher)
. -
toCase
Convertsinput
string to using the givenCaseFormat
.input
can be insnake_case
,lowerCamelCase
,UpperCamelCase
,CONSTANT_CASE
,dash-case
or any combination thereof. For example:toCase(LOWER_CAMEL, "user_id") => "userId" toCase(LOWER_HYPHEN, "UserID") => "user-id" toCase(UPPER_UNDERSCORE, "orderId") => "ORDER_ID" toCase(LOWER_UNDERSCORE, "primaryUser.userId") => "primary_user.user_id"
Given that
CaseFormat
only handles ascii, characters outside of the range of[a-zA-Z0-9_-]
(e.g. whitespaces, parenthesis, non-ascii) are passed through as is. If you need to support non-ascii camel case such as Greek upper case ('Β') and lower case ('β'), consider usingbreakCase(java.lang.CharSequence)
to break up words in the source and then apply target casing manually using e.g.Character.toLowerCase(char)
.
-