Java中是否内置了一个函数,使字符串中每个单词的第一个字符大写,而不影响其他字符?
例子:
乔恩·斯基特->乔恩·斯基特
miles o'Brien -> miles o'Brien (B仍然是大写,这排除了Title Case)
老麦当劳->
*(老麦当劳也会被发现,但我不指望它有那么聪明。)
快速浏览一下Java字符串文档就会发现只有toUpperCase()和toLowerCase(),它们当然没有提供所需的行为。当然,谷歌的结果主要是这两个函数。它看起来像一个已经被发明出来的轮子,所以问一下也无妨,这样我以后就可以用它了。
来自Java 9+
你可以像这样使用String::replaceAll:
public static void upperCaseAllFirstCharacter(String text) {
String regex = "\\b(.)(.*?)\\b";
String result = Pattern.compile(regex).matcher(text).replaceAll(
matche -> matche.group(1).toUpperCase() + matche.group(2)
);
System.out.println(result);
}
例子:
upperCaseAllFirstCharacter("hello this is Just a test");
输出
Hello This Is Just A Test
这是我的解决方案。
今晚我偶然发现了这个问题,并决定研究一下。我发现Neelam Singh的答案几乎在那里,所以我决定解决这个问题(断在空字符串上),导致系统崩溃。
您正在寻找的方法名为capString(String s)。
它把“这里才早上5点”变成了“这里才早上5点”。
代码注释得很好,喜欢吧。
package com.lincolnwdaniel.interactivestory.model;
public class StringS {
/**
* @param s is a string of any length, ideally only one word
* @return a capitalized string.
* only the first letter of the string is made to uppercase
*/
public static String capSingleWord(String s) {
if(s.isEmpty() || s.length()<2) {
return Character.toUpperCase(s.charAt(0))+"";
}
else {
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
}
/**
*
* @param s is a string of any length
* @return a title cased string.
* All first letter of each word is made to uppercase
*/
public static String capString(String s) {
// Check if the string is empty, if it is, return it immediately
if(s.isEmpty()){
return s;
}
// Split string on space and create array of words
String[] arr = s.split(" ");
// Create a string buffer to hold the new capitalized string
StringBuffer sb = new StringBuffer();
// Check if the array is empty (would be caused by the passage of s as an empty string [i.g "" or " "],
// If it is, return the original string immediately
if( arr.length < 1 ){
return s;
}
for (int i = 0; i < arr.length; i++) {
sb.append(Character.toUpperCase(arr[i].charAt(0)))
.append(arr[i].substring(1)).append(" ");
}
return sb.toString().trim();
}
}
如果你需要大写标题,这可能很有用。它将每个由" "分隔的子字符串大写,除了"a"或"the"之类的指定字符串。我还没有运行它,因为已经晚了,不过应该没问题。一次使用Apache Commons StringUtils.join()。如果你愿意,你可以用一个简单的循环来代替它。
private static String capitalize(String string) {
if (string == null) return null;
String[] wordArray = string.split(" "); // Split string to analyze word by word.
int i = 0;
lowercase:
for (String word : wordArray) {
if (word != wordArray[0]) { // First word always in capital
String [] lowercaseWords = {"a", "an", "as", "and", "although", "at", "because", "but", "by", "for", "in", "nor", "of", "on", "or", "so", "the", "to", "up", "yet"};
for (String word2 : lowercaseWords) {
if (word.equals(word2)) {
wordArray[i] = word;
i++;
continue lowercase;
}
}
}
char[] characterArray = word.toCharArray();
characterArray[0] = Character.toTitleCase(characterArray[0]);
wordArray[i] = new String(characterArray);
i++;
}
return StringUtils.join(wordArray, " "); // Re-join string
}
我做了这个小类,可以用来大写一个句子中的每个单词。如果不是空格,可以更改字符串中的单词分隔符。
package com.ecnews.ecnews_v01.Helpers;
public class Capitalize {
String sentence;
String separator = " ";
public Capitalize(String sentence) {
this.sentence = sentence;
}
public void setSeparator(String separator) {
this.separator = separator;
}
public String getCapitalized() {
StringBuilder capitalized = new StringBuilder("");
for (String word : sentence.split(separator)) {
capitalized.append(separator+Character.toUpperCase(word.charAt(0)) + word.substring(1));
}
return capitalized.toString().trim();
}
}
例子:
String sourceName = new Capitalize("this is a test"). get资本化();
sourceName将是“This Is A Test”