编写一个Java程序,将句子中每个单词的首字母大写
Write a Java program to capitalize the first letter of each word in a sentence
Write a Java program to capitalize the first letter of each word in a sentence.
为此,我编写了一个 java 程序,但它抛出了一个错误
"result of string.replace(),is ignored";
我的代码
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string :");
String a = sc.nextLine();
for(int i = 0; i < a.length(); i++) {
if(a.charAt(i) == ' ') {
char c;
char d;
c = a.charAt((i + 1));
d = Character.toUpperCase(c);
a.replace(a.charAt((i + 1)),d);
}
}
System.out.println(a);
}
}
不知道哪里弄错了
Java 的 String.replace
方法不会对字符串进行突变,它 returns 一个执行了替换的新字符串。
参见 here。
您需要重新考虑您的设计。一种方法是复制字符串,然后扫描原始字符串,同时逐步替换副本。
像这样:
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string :");
String a = sc.nextLine();
String b = a;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == ' ') {
char c = a.charAt((i + 1));
char d = Character.toUpperCase(c);
b = b.replace(a.charAt(i + 1), d);
}
}
System.out.println(a);
}
}
您甚至可以像这样替换 a
:
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string :");
String a = sc.nextLine();
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == ' ') {
char c = a.charAt((i + 1));
char d = Character.toUpperCase(c);
a = a.replace(a.charAt(i + 1), d);
}
}
System.out.println(a);
}
}
使用replaceAll
String a = "Write a Java program to capitalize the first letter of each word in a sentence.";
String b = Pattern
.compile("([^a-zA-Z]*)([a-zA-Z]+)")
.matcher(a)
.replaceAll(w ->
w.group(1) + w.group(2).substring(0, 1).toUpperCase() + w.group(2).substring(1));
System.out.println(b);
有输出
Write A Java Program To Capitalize The First Letter Of Each Word In A Sentence.
(注:词义定义不明确,根据需要改见例https://www.jrebel.com/blog/java-regular-expressions-cheat-sheet)
这里是 Java 的流畅代码,可以在完全不使用替换方法的情况下执行相同的操作,
import java.util.Arrays;
import java.util.stream.Collectors;
public class HelloWorld{
public static void main(String []args){
String data = "Hello world is my 1st program, wow i am amazed";
String output = Arrays.stream(data.split(" "))
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1))
.collect(Collectors.joining(" "));
System.out.println("Output: " + output);
}
}
输出如下,
Output: Hello World Is My 1st Program, Wow I Am Amazed
您可以在 https://www.tutorialspoint.com/compile_java8_online.php
在线尝试上述解决方案
Write a Java program to capitalize the first letter of each word in a sentence.
为此,我编写了一个 java 程序,但它抛出了一个错误
"result of string.replace(),is ignored";
我的代码
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string :");
String a = sc.nextLine();
for(int i = 0; i < a.length(); i++) {
if(a.charAt(i) == ' ') {
char c;
char d;
c = a.charAt((i + 1));
d = Character.toUpperCase(c);
a.replace(a.charAt((i + 1)),d);
}
}
System.out.println(a);
}
}
不知道哪里弄错了
Java 的 String.replace
方法不会对字符串进行突变,它 returns 一个执行了替换的新字符串。
参见 here。
您需要重新考虑您的设计。一种方法是复制字符串,然后扫描原始字符串,同时逐步替换副本。
像这样:
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string :");
String a = sc.nextLine();
String b = a;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == ' ') {
char c = a.charAt((i + 1));
char d = Character.toUpperCase(c);
b = b.replace(a.charAt(i + 1), d);
}
}
System.out.println(a);
}
}
您甚至可以像这样替换 a
:
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string :");
String a = sc.nextLine();
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == ' ') {
char c = a.charAt((i + 1));
char d = Character.toUpperCase(c);
a = a.replace(a.charAt(i + 1), d);
}
}
System.out.println(a);
}
}
使用replaceAll
String a = "Write a Java program to capitalize the first letter of each word in a sentence.";
String b = Pattern
.compile("([^a-zA-Z]*)([a-zA-Z]+)")
.matcher(a)
.replaceAll(w ->
w.group(1) + w.group(2).substring(0, 1).toUpperCase() + w.group(2).substring(1));
System.out.println(b);
有输出
Write A Java Program To Capitalize The First Letter Of Each Word In A Sentence.
(注:词义定义不明确,根据需要改见例https://www.jrebel.com/blog/java-regular-expressions-cheat-sheet)
这里是 Java 的流畅代码,可以在完全不使用替换方法的情况下执行相同的操作,
import java.util.Arrays;
import java.util.stream.Collectors;
public class HelloWorld{
public static void main(String []args){
String data = "Hello world is my 1st program, wow i am amazed";
String output = Arrays.stream(data.split(" "))
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1))
.collect(Collectors.joining(" "));
System.out.println("Output: " + output);
}
}
输出如下,
Output: Hello World Is My 1st Program, Wow I Am Amazed
您可以在 https://www.tutorialspoint.com/compile_java8_online.php
在线尝试上述解决方案