将字符串转换为标题案例

我是Java的初学者,试图编写一个程序来将字符串转换为标题案例。 例如,如果String s = "my name is milind" ,则输出应为"My Name Is Milind"

 import java.util.*; class TitleCase { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("ent"); String s=in.nextLine(); String str =""; char a ; for(int i =0;i<s.length()-1;i++) { a = s.charAt(i); if(a==' ') { str = str+(Character.toUpperCase(s.charAt(i+1))); } else { str =str+(Character.toLowerCase(a)); } } //for(int i =0; i<s.length();i++) //{ System.out.println(str); //} } } 

您正在尝试将输入的每个单词都大写。 所以你必须做以下步骤:

  1. 将单词分开
  2. 把每个词都用掉
  3. 把它们放在一起
  4. 打印出来

示例代码:

  public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.println("ent"); String s=in.nextLine(); //now your input string is storred inside s. //next we have to separate the words. //here i am using the split method (split on each space); String[] words = s.split(" "); //next step is to do the capitalizing for each word //so use a loop to itarate through the array for(int i = 0; i< words.length; i++){ //we will save the capitalized word in the same place again //first, geht the character on first position //(words[i].charAt(0)) //next, convert it to upercase (Character.toUppercase()) //then add the rest of the word (words[i].substring(1)) //and store the output back in the array (words[i] = ...) words[i] = Character.toUpperCase(words[i].charAt(0)) + [i].substring(1); } //now we have to make a string out of the array, for that we have to // seprate the words with a space again //you can do this in the same loop, when you are capitalizing the // words! String out = ""; for(int i = 0; i 

使用Java 8流:

 String titleCase = (new ArrayList<>(Arrays.asList(inputString.toLowerCase().split(" ")))) .stream() .map(word -> Character.toTitleCase(word.charAt(0)) + word.substring(1)) .collect(Collectors.joining(" ")); 

问题在于你添加字符的方式。 看看你的if条件:

 a = s.charAt(i); if(a==' ') { // Here you are adding not the current character, but the NEXT character. str = str+(Character.toUpperCase(s.charAt(i+1))); } else { // Here you are adding the current character. str =str+(Character.toLowerCase(a)); } 

作为此条件的结果,如果输入字符串包含空格,则将跳过一个字符,然后重复另一个已添加的字符。

另外,你没有遍历整个字符串,因为你的循环条件转到s.length()-1 。 将其更改为s.length() 。 但是,如果这样做,如果输入字符串以空格结尾,则可能会遇到exception(因为您将尝试在超出范围的索引处检查字符)。

这是固定代码的样子:

 public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("ent"); String s=in.nextLine(); String str =""; char a ; for(int i =0;i 

注意:我只修复了您提供的代码。 但是,我不确定它是否按照你想要的方式工作 - 字符串的第一个字符仍然是它开始的任何情况。你的条件只有大写字母前面有一个空格。

您想要更改String的每个单词的第一个字母的大小写。

为此,我将按照以下步骤操作:

  • 用单词拆分String:参见String.split(separator)
  • 检索每个单词的第一个字母:请参阅String.charAt(index)
  • 检索它的大写版本:你使用的Character.toUpperCase(char)是完美的
  • 将大写字母与单词的其余部分连接起来:连接运算符( + )和String.substring
  • 从大写单词创建一个新的String:请参阅String.join(separator)

代码高尔夫变化…我挑战任何人使它比这简单:

 public String titleCase(String str) { return Arrays .stream(str.split(" ")) .map(String::toLowerCase) .map(StringUtils::capitalize) .collect(Collectors.joining(" ")); } 

顺便说一句:Unicode区分三种情况:小写,大写和标题情况。 虽然对于英语没有关系,但是还有其他语言,其中一个字符的标题大小写与大写版本不匹配。 所以你应该使用

 Character.toTitleCase(ch) 

而不是Character.toUpperCase(ch)的第一个字母。

Unicode中有三个字符大小写:upper,lower和title。 大多数人都熟悉大写和小写。 Titlecase区分由多个组成部分组成的字符,并且在标题中使用时会以不同方式书写,其中单词中的第一个字母传统上是大写的。 例如,在字符串“ljepotica”中,[2]第一个字母是小写字母lj(\ u01C9,扩展拉丁字符集中用于编写克罗地亚有向图的字母)。 如果单词出现在书名中,并且你希望每个单词的第一个字母都是大写的,那么正确的过程就是在每个单词的第一个字母上使用toTitleCase,给你“Ljepotica”(使用Lj,这是\ u01C8)。 如果你错误地使用了toUcapCase,你会得到错误的字符串“LJepotica”(使用LJ,即\ u01C7)。

[Java™编程语言,第四版,作者:James Gosling,Ken Arnold,David Holmes(Prentice Hall)。 版权所有2006 Sun Microsystems,Inc.,9780321349804]

 import java.util.Scanner; public class TitleCase { public static void main(String[] args) { System.out.println("please enter the string"); Scanner sc1 = new Scanner(System.in); String str = sc1.nextLine(); //whatever the format entered by user, converting it into lowercase str = str.toLowerCase(); // converting string to char array for //performing operation on individual elements char ch[] = str.toCharArray(); System.out.println("==============="); System.out.println(str); System.out.println("==============="); //First letter of senetence must be uppercase System.out.print((char) (ch[0] - 32)); for (int i = 1; i < ch.length; i++) { if (ch[i] == ' ') { System.out.print(" " + (char) (ch[i + 1] - 32)); //considering next variable after space i++; continue; } System.out.print(ch[i]); } } } 

您可以使用lamda代替 –

 String titalName = Arrays.stream(names.split(" ")) .map(E -> String.valueOf(E.charAt(0))+E.substring(1)) .reduce(" ", String::concat);