- 使用 javascript 我到目前为止是在此处输入图像描述
所以如果我要输入“祝你和你的家人节日快乐”,我希望我的输出是“祝你和你的家人节日快乐”
这是我使用流的 Java 解决方案:
用于输入
“祝您和您的家人节日快乐”
输出是
“祝你和你的家人节日快乐”
public String convertToLowerCase(String str) {
String delimiter = " ";
String[] wordList = str.split(delimiter);
String lastWord = wordList[wordList.length - 1];
return Arrays.stream(wordList).map(String::toLowerCase).limit(wordList.length - 1).collect(Collectors.joining(delimiter)) + delimiter + lastWord;
}
这是一个Javascript解决方案
let myString = "Happy Holidays To YOU and YOUR Family"
let myFunction = (input) => {
let arr = input.toLowerCase().split(" ");
let str = arr[arr.length-1];
arr[arr.length-1] = str[0].toUpperCase() + str.slice(1).toLowerCase();
return arr.join(" ");
}
let newString = myFunction(myString)
console.log(newString)
这是答案:
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
String myString = "Happy Holidays To YOU and YOUR Family";
try{
System.out.println(stringLowerCaseExpectLastWord(myString));
} catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
/**
* Upper first char at last word string.
*
* @param myString the my string
* @return the string
*/
public static String stringLowerCaseExpectLastWord(String myString) {
myString = myString.trim().toLowerCase();
int i = myString.lastIndexOf(" ");
if(i==-1)
throw new IllegalArgumentException("No spaces in input");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(myString.substring(0, i)
.concat(" "));
if(myString.length()==i+i){
stringBuilder.append(myString.substring(i + 1).toUpperCase());
}
else {
stringBuilder.append(myString.substring(i+1, i+2).toUpperCase()
.concat(myString.substring(i+2)));
}
return stringBuilder.toString();
}