Transform a string between
camelCase
,PascalCase
,Capital Case
,snake_case
,param-case
,CONSTANT_CASE
and others.
These functions come with package:change_case
Transform into a string with the separator denoted by the next word capitalized.
'test string'.toCamelCase(); // 'testString'
Transform into a space separated string with each word capitalized.
'test string'.toCapitalCase(); // 'Test String'
Transform into upper case string with an underscore between words.
'test string'.toConstantCase(); // 'TEST_STRING'
Transform into a lower case string with a period between words.
'test string'.toDotCase(); // 'test.string'
Transform into a dash separated string of capitalized words.
'test string'.toHeaderCase(); // 'Test-String'
Transform into a lower cased string with spaces between words.
'testString'.toNoCase(); // 'test string'
Transform into a lower cased string with dashes between words.
'test string'.toParamCase(); // 'test-string'
Transform into a string of capitalized words without separators.
'test string'.toPascalCase(); // 'TestString'
Transform into a lower case string with slashes between words.
'test string'.toPathCase(); // 'test/string'
// provide a separator override default '/'
'test string'.toPathCase(r'\'); // 'test\string'
Transform into a lower case with spaces between words, then capitalize the string.
'testString'.toSentenceCase(); // 'Test string'
Transform into a lower case string with underscores between words.
'test string'.toSnakeCase(); // 'test_string'
Transform a string into title case following English rules.
'a simple test'.toTitleCase(); // 'A Simple Test'
Transform a string by swapping every character from upper to lower case, or lower to upper case.
'Test String'.toSwapCase(); // 'tEST sTRING'
Returns
true
if the string is lower case only.
'test string'.isLowerCase(); // true
Returns
true
if the string is upper case only.
'test string'.isUpperCase(); // false
Transforms the string with the first character in lower cased.
'TEST'.toLowerFirstCase(); // 'tEST'
Transforms the string with the first character in upper cased.
'test'.toUpperFirstCase(); // 'Test'
Transform into a string with random capitalization applied.
'Test String'.toSpongeCase(); // 'tEst stRINg'
package:change_case
has a configuration file,ChangeCaseConfig
, that you can use to updatesplitPatterns
,stripPatterns
, and theplaceholder
which will be applied to most function listed above.
I don't suggest changing these unless you know what you are doing š
Change case uses RegExp
to split & replace characters of the string.
While they are great at what they do, you may want to update or add new patterns.
These patterns can be updated by using the ChangeCaseConfig
.setUp(splitPatterns: [...], stripPatterns: [...])
The default patterns are:
Note:
(?:ā¢)*
is used to match any "ā¢
" (placeholder
) character.
-
Split
-
ChangeCaseConfig.lowerOrNumToUpperPattern
// matches lowercase or numeric char to uppercase char RegExp('([a-z0-9])(?:ā¢)*([A-Z])')
-
ChangeCaseConfig.lowerOrNumToUpperPattern
// matches uppercase char to uppercase followed by lowercase char RegExp('([A-Z])(?:ā¢)*([A-Z][a-z])')
-
-
Strip
-
ChangeCaseConfig.upperToLowerPattern
// matches any non-alphanumeric char RegExp('[^A-Z0-9]+', caseSensitive: false)
-
ChangeCaseConfig.lowerToNumOrUpperPattern
(Not included in default, but can be used to add or replace)// matches lowercase char to numeric or uppercase char // changes the behavior to `word2019 -> word 2019` and `minifyURLs -> minify urls` RegExp('([a-z])([A-Z0-9])')
-
can be configured with
ChangeCaseConfig
.setUp(placeholder: ...)
The placeholder
is a string that is added and used as a reference to configure each case.
The default placeholder is "ā¢
"
For example:
final string = 'test stringCase';
print(string.toCamelCase()); // expected: 'testStringCase'
// runs splitPatterns, returns "test stringā¢Case"
// runs stripPatterns, returns "testā¢stringā¢Case"
// runs camel case logic, returns "testStringCase"
This string needs to be a unique character that is not used in the string.