CaseFormat to detect format of a string (value)
See original GitHub issueIt’s nice to have a function to test whether a particular string is in which case format.
In a tool application, a string will provided by external code. And if the value matches some known case format, It will then be normalised to a standard format. e.g. if input matches UPPER_UNDERSCORE
and LOWER_UNDERSCORE
will both converted to LOWER_CAMEL
.
Some ideas illustrated as below:
/**
* Tests which CaseFormat the <code>value</code> is.
*
* @param value
* @return the case format for the value if found. <code>null</code> if no matches found.
*/
@Nullable
public static CaseFormat test(@Nonnull String value) {
// in worst case, may need to enumerate all format
// some quick test, like first detect underscore then check cases etc may help.
return CaseFormat.Test;
}
/**
* Tests whether <code>value</code> is in this case format.
*
* @param value
* @return <code>true</code> if <code>value</code> is matching case format.
*/
abstract boolean matches(@Nonnull String value);
Issue Analytics
- State:
- Created 8 years ago
- Comments:14 (4 by maintainers)
Top Results From Across the Web
CaseFormat Class | Guava | Java - GeeksforGeeks
CaseFormat is a utility class for converting between various ASCII case formats. Behavior is undefined for non-ASCII input.
Read more >CaseFormat (Guava: Google Core Libraries for Java 19.0 API)
Converts the specified String str from this format to the specified format . Returns the enum constant of this type with the specified...
Read more >What is the simplest way to convert a Java string from all caps ...
Another option is using Google Guava's com.google.common.base.CaseFormat. George Hawkins left a comment with this example of usage: CaseFormat.
Read more >Convert a String to Camel Case - Baeldung
In this tutorial, we'll look at how to write some custom string ... Guava has a utility class, CaseFormat, for format conversion: String...
Read more >How to format numbers, dates, enums, and other types in .NET
The format strings passed to an enumeration value's ToString method determine whether the value is displayed using its string name (the "G" ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+5
I don’t really care what the current case format for a given incoming string is, but whatever it is, I want to convert it to a specific case format. I’m not sure what systems could make use of finding out what format a string currently is in, but I would think that there’s a not-small class of software that would be interested in taking in a string from some external, possibly untrusted, source and converting it to a specific case format.
We have a system that uses a case format for different fields on a variety of data for a variety of scenarios (usually it’s display scenarios we care about), and we are not always in control of the input.
The alternative is to run through a bunch of guesses on the source format and see which conversion to a target format yields an identical string. Only then can the conversion be made.
+1 This will help.