我正在尝试大写字符串的第一个字符。我看过关于堆栈溢出的其他文章,并尝试了Apache通用包。但是,输出保持较低的大小写,且未修改。这是我的密码
package name;
import java.util.Scanner;
import java.lang.Object;
import org.apache.commons.lang3.text.WordUtils;
public class Name {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("What is your first name?");
String first = input.nextLine();
System.out.println("What is your last name?");
String last = input.nextLine();
String full = (first + " " + last);
WordUtils.capitalize(full);
System.out.println("Your name is " + full);
input.close();
}
}我也试过
System.out.println("What is your first name?");
String first = input.nextLine();
WordUtils.capitalize(first);
System.out.println("What is your last name?");
String last = input.nextLine();
WordUtils.capitalize(last);
System.out.println("Your name is " + first + last);我试过使用capitalzieFully,但也没有结果。(我知道对象没有被使用,我只是尝试将其作为测试导入)。
发布于 2013-10-03 11:55:12
字符串在java中是不可变的。
first= WordUtils.capitalize(first);因此,您必须将其重新分配给由first方法返回的capitalize。
String first= "test";
WordUtils.capitalize(first);
//Above method returns a new String,you are not receiving that
// Still first is "test" because String is immutable.
first= WordUtils.capitalize(first);
//Now first = "TEST"在其他地方也是如此。
发布于 2013-10-03 11:54:34
试一试
last = WordUtils.capitalize(last);方法返回一个字符串,字符串是不可变的。
发布于 2013-10-03 11:55:30
full = WordUtils.capitalize(full);您需要将修改后的字符串重新分配回自身,以使更改反映出来。因为字符串是不可变的。
或者,如果您不想使用任何外部库,您可以这样做:
full = Character.toUpperCase(full.charAt(0)) + full.substring(1);https://stackoverflow.com/questions/19158533
复制相似问题