[英]How to Convert Upper case string to Camel Case and keeping the Acronyms as it is?
I am working on H2 DB and trying to create a Java function to convert Uppercase Strings into Camelcase for my dataset. 我正在研究H2 DB,并尝试创建Java函数以将大写字符串转换为我的数据集的Camelcase。 Although, I am relatively a newbie in JAVA, but after some research I was able to arrive at the solution below. 虽然,我是JAVA的新手,但是经过一番研究,我能够找到以下解决方案。
DROP ALIAS toCamelCase if exists;
CREATE ALIAS toCamelCase AS $$
String toCamelCase(String s)
{
String[] parts = s.split("\\s+");
String camelCaseString = "";
for (String part : parts)
{
if(part != null && part.trim() != "")
{
camelCaseString = camelCaseString + part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase() + " ";
}
}
return camelCaseString;
} $$;
Now this does solve 95 percent of my problem, but the issue is that there are some elements in the dataset which are acronyms or short forms, for eg: FTE or TBB which as a result of this code are being transformed into Fte or Tbb . 现在,这确实解决了我95%的问题,但是问题是数据集中有些元素是缩写或缩写形式,例如: FTE或TBB ,由于此代码而被转换为Fte或Tbb 。 Can I achieve the same result but this time keeping the acronyms or short forms in their original form for the strings of data I am working on? 我能否获得相同的结果,但是这次将首字母缩写词或缩写形式保留在我正在处理的数据字符串的原始形式中?
Appreciate the help! 感谢帮助!
If you want to prevent acronyms from being changed, create a separate .txt file with a list on known acronyms. 如果要防止更改首字母缩写词,请创建一个单独的.txt文件,其中包含已知首字母缩写词的列表。 Then you can prevent it from being changed by simply doing this: 然后,您可以通过执行以下操作来防止更改它:
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null){
if (part == line)
break;
else
camelCaseString = camelCaseString + part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase() + " ";
}
}
Replace this to be the contents of the for each loop. 将其替换为每个循环的内容。
Remember to set "file" to a File object that contains the .txt file. 记住将“文件”设置为包含.txt文件的File对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.