简体   繁体   English

如何将大写字符串转换为Camel Case并保持首字母缩略词不变?

[英]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%的问题,但是问题是数据集中有些元素是缩写或缩写形式,例如: FTETBB ,由于此代码而被转换为FteTbb 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.

相关问题 尝试分别将字符串转换为小写/大写 - Trying To convert String to lower/upper case respectively 如何在phpstorms(基于速度)文件中将字符串转换为驼峰情况? - How to convert a string to camel case in phpstorms (velocity based) file tempates? Java如何在字符串数组中将小写字符串值转换为大写字符串值 - Java how do you convert lower case string values to upper case ones in a string array 如何将小写字母转换为大写字母 & 以及将大写字母转换为小写字母 - how to convert Lower case letters to upper case letters & and upper case letters to lower case letters 我正在尝试将字符串从小写转换为大写,将大写转换为小写 - I am trying to convert a string from lower case to upper case and upper case to lower casee 如何在字符串中存在的每个特殊字符之后将小写字符转换为大写? - How to convert lower case character into upper case after each special character present in the String? 如何在 REST API 中将骆驼大小写转换为带下划线的小写字母? - How to convert camel case to lower case with underscores in a REST API? 我应该如何将 JSON 有效载荷的大小写转换为小驼峰大小写? - How should I convert the case of a JSON payload to lower camel case? 如何将一个字符串转换为大写和小写 - How to manipulate one string to upper case and lower case 大写转换 - 将字符串中每个单词的第一个字母转换为大写 - Upper case conversion - convert the first letter of each word in the string to uppercase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM