我必须让我的程序大写一个句子的第一个字母,出于某种原因,我的代码无法工作。我不知道为什么 - 我的代码看起来应该是可行的,而且我更懂得在我的经验不足中责怪Java中的一个“可能”错误。这是我到目前为止:我怎样才能让我的程序大写一个句子的第一个字母?
public class SentenceFormator {
public static String format(String str){
String fmts = str;
String temp;
//Finds first subString makes it capitalized and replaces it in original String.
temp = fmts.substring(0, 1).toUpperCase();
fmts = fmts.replaceFirst(fmts.substring(0,1), temp);
//Makes all other letters after beginning of sentence lower-case.
fmts = fmts.substring(1,str.length()-1).toLowerCase();
//Capitalizes all i's in the String.
fmts = fmts.replaceAll(" i ", " I ");
//Take away white spaces at the end and beginning of the String.
fmts = fmts.trim();
//Add punctuation if not already there.
if(!fmts.endsWith(".")||fmts.endsWith("!")||fmts.endsWith("?")){
fmts = fmts.concat(".");
}
//Takes away
for(int i = 0; i<str.length();i++){
}
return fmts;
}
}
难道只是我还是你的标题不同的F /什么你问?所以你只需要做“带走”功能?如果是这样,请更改标题 – Coffee
@Adel请在评论中使用整个单词。这不是一条短信:-) –
首先定义“句子”。 –