【问题标题】:Make second word capital in a string在字符串中使第二个单词大写
【发布时间】:2022-11-06 00:26:46
【问题描述】:

我真的不知道如何解释这个问题。我确实在打包后导入了扫描仪。我不确定您是否可以堆叠方法,如果可以,我肯定做错了。

Scanner console = new Scanner(System.in);
System.out.print("Enter your name: ");

String name = console.next();
name.trim();
name.toUpperCase(name.substring(name.charAt(name.indexOf(" "))));

System.out.println("Your name is: " + name);

【问题讨论】:

  • 只需使用 split 方法来获取所有单独的单词。
  • 我怎么做?我几周前才开始学习。
  • String 是不可变的。 name.trim() 没有任何意义
  • @ThomasHuppert,您能否澄清一下您的意见和期望?
  • 强调@ChristophS. 的观点:String#trim(或任何带有String 返回的字符串操作,如String#toUpperCase)很可能会返回一个新的字符串,保持旧的不变。您需要重新分配结果。

标签: java string substring indexof charat


【解决方案1】:

只需根据indexOf(" ") 将字符串拆分为第一个单词和第二个单词

使用toUpperCase 将第二个单词大写

使用+ 将两个单词连接在一起

name = name.substring(0, name.indexOf(" ")) + name.substring(name.indexOf(" ")).toUpperCase();

注意:不确定是否需要处理无效输入,但此代码仅在输入有效的两个单词名称且中间有空格的情况下才有效

另外,确保将console.next() 更改为console.nextLine() 以确保您检索到整行输入

【讨论】:

    【解决方案2】:
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = console.nextLine();
        String[] words = name.split(" ");
        words[1] = capitalizeWord(words[1]);
        name = String.join(" ", words);
        System.out.println("Your name is: " + name);
    }
    
    private static String capitalizeWord(String s) {
        s = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
        return s;
    }
    

    首先,您将输入拆分为String 数组。然后替换第二个(索引 1)元素的第一个字符并将数组连接回String

    输入:john doe,输出:john doe

    【讨论】:

      【解决方案3】:

      String is immutable 在 java 中,所以如果你想“改变”一个 Strig 变量值,你需要将它重新分配给它自己。我认为准备两个以上输入的好方法,很多人都有中间名,例如 此函数将输入分成几部分,然后将第一个字母变为大写,然后在将它们与空格连接后返回整个名称。

       public String capitalizeName(String name) {
          String[] nameParts = name.split(" ");
          for (int i = 0; i < nameParts.length; i++) {
              nameParts[i] = nameParts[i].substring(0, 1).toUpperCase() + nameParts[i].substring(1);
          }
          return String.join(" ", nameParts);
      }
      

      【讨论】:

        【解决方案4】:

        String不可变在爪哇。

        简单点!!!

        public static void main(String... args) throws IOException {
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter your first name and last name: ");
        
            String firstName = upperCaseFirstLetter(scan.next().trim());
            String lastName = upperCaseFirstLetter(scan.next().trim());
        
            System.out.println("Your name first name: " + firstName);
            System.out.println("Your name last name: " + lastName);
        }
        
        private static String upperCaseFirstLetter(String name) {
            return Character.toUpperCase(name.charAt(0)) + name.substring(1);
        }
        

        【讨论】:

          【解决方案5】:

          一种解决方案是使用split(regex)-方法将String 设为String 数组。它将一个字符串拆分为一个字符串数组,并在正则表达式中将它们分解。 例如:

          String text = "This is a text.";
          String textArray = text.split(" ");
          
          for(String element : textArray)
          {
              System.out.println(element);
          }
          

          将打印

          This
          is
          a
          text.
          

          如果你有这样的 String[],你可以选择第二个 String(数组的索引 1)并将其大写。例如,您可以在 foreach 循环中执行此操作。

          String text = "This is a text.";
          text = text.trim(); // if you want to trim it.
          String[] textArray = text.split(" ");
          
          String newText = "";
              
          int index = 0;
              
          for(String element : textArray)
          {
              if(index == 1)
              {
                  element = element.toUpperCase();
              }
              newText = newText + element + " ";
              index++;
          }
              
          System.out.println(newText);
          

          如果你想处理错误,你可以像这样放入一个 try-catch-block 。

          try
          {
              [Your code]
          }
          catch (Exception e)
          {
              System.out.println("An error occured.");
          }
          

          当然,这不是一个很短的方法。但是,它很容易理解,甚至可以处理由几个单词组成的字符串。

          【讨论】:

            猜你喜欢
            • 2014-04-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-25
            • 1970-01-01
            • 1970-01-01
            • 2016-09-06
            相关资源
            最近更新 更多