我正在编写一个 java DAO 类,它将读取表的元数据并转换列名。我在使用 REGEX 进行基于以下规则的转换时遇到问题。
- remove first 2 characters
- convert all letters to lowercase with the exception of the first character after the underscore (_) symbol
- remove underscore
所以...
X_H -> h
X_THE_FIND -> theFind
Y_SCORE_CARD -> scoreCard
Y_PARTNER -> partner
(注意:数据库列仅包含大写字母,并且可能包含一个或多个下划线字符。前 2 个字符始终为 X_ 或 Y_。列名称始终至少包含 3 个字符。)
最佳答案
使用WordUtils in the Apache Commons lang library只需 1 行即可完成:
// assuming str is your String
str = WordUtils.capitalizeFully(str.substring(2), new char[]{'_'}).replace("_", "");
关于java - REGEX.. 将 X_FOO_BAR 转换为 fooBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254282/