public static String capitalise(String str)
{
if (str != null || !"".equals(str))
{
char chr=str.charAt(0);
String check= Character.toString(chr);
String check1= check.toUpperCase();
char chr1=check1.charAt(0);
str.replace(chr, chr1);
return str;
}
else
{
System.out.println("Not a valid String");
}
return str;
}
问问题
119 次
4 回答
2
您的问题是您没有从 str.replace 返回结果。字符串是不可变的
于 2012-10-30T06:31:43.253 回答
2
为什么你不简单地使用:
String str = "THIS IS my TEST strinG";
String my_string = str.substring(0,1).toUpperCase() + str.substring(1).toLowercase();
于 2012-10-30T06:34:14.643 回答
1
请确保您必须为 String 分配值
str = str.replace(chr, chr1);
于 2012-10-30T06:32:28.420 回答
0
str.replace(chr, chr1);
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. replace
But you are not storing that value. So you can use following code
str = str.replace(chr, chr1);
于 2012-10-30T06:37:56.003 回答