请在标题大小写中格式化名称 Java 帮助吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12656941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format name in title case Java help please?
提问by Mohammed Ahmed
so i have to write a java code to :
所以我必须写一个java代码来:
- Input a name
- Format name in title case
- Input second name
- Format name in title case
- Display them in alphabet order
- 输入名称
- 标题大小写中的格式名称
- 输入第二个名字
- 标题大小写中的格式名称
- 按字母顺序显示它们
i know that The Java Character class has the methods isLowerCase(), isUpperCase, toLowerCase() and toUpperCase(), which you can use in reviewing a string, character by character. If the first character is lowercase, convert it to uppercase, and for each succeeding character, if the character is uppercase, convert it to lowercase.
我知道 Java Character 类具有方法 isLowerCase()、isUpperCase、toLowerCase() 和 toUpperCase(),您可以使用它们来逐个字符地查看字符串。如果第一个字符是小写,则将其转换为大写,对于每个后续字符,如果该字符是大写,则将其转换为小写。
the question is how i check each letter ? what kind of variables and strings should it be contained ? can you please help?
问题是我如何检查每个字母?它应该包含什么样的变量和字符串?你能帮忙吗?
回答by Rohit Jain
You should use StringBuilder
, whenver dealing with String manipulation.. This way, you end up creating lesser number of objects..
您应该在StringBuilder
处理字符串操作时使用 .. 这样,您最终会创建较少数量的对象..
StringBuilder s1 = new StringBuilder("rohit");
StringBuilder s2 = new StringBuilder("jain");
s1.replace(0, s1.length(), s1.toString().toLowerCase());
s2.replace(0, s2.length(), s2.toString().toLowerCase());
s1.setCharAt(0, Character.toTitleCase(s1.charAt(0)));
s2.setCharAt(0, Character.toTitleCase(s2.charAt(0)));
if (s1.toString().compareTo(s2.toString()) >= 0) {
System.out.println(s2 + " " + s1);
} else {
System.out.println(s1 + " " + s2);
}
回答by Jo?o Silva
You can convert the first character to uppercase, and then lowercase the remainder of the string:
您可以将第一个字符转换为大写,然后将字符串的其余部分小写:
String name = "jOhN";
name = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
System.out.println(name); // John
回答by Meredith
For traversing Strings using only the String class, iterate through each character in a string.
要仅使用 String 类遍历字符串,请遍历字符串中的每个字符。
String s = "tester";
int size = s.length(); // length() is the number of characters in the string
for( int i = 0; i < size; i++) {
// s.charAt(i) gets the character at the ith code point.
}
This questionanswers how to "change" a String - you can't. The StringBuilder class provides convenient methods for editing characters at specific indices though.
这个问题回答了如何“改变”一个字符串——你不能。StringBuilder 类提供了方便的方法来编辑特定索引处的字符。
It looks like you want to make sure all names are properly capitalized, e.g.: "martin ye" -> "Martin Ye" , in which case you'll want to traverse the String input to make sure the first character of the String and characters after a space are capitalized.
看起来您想确保所有名称都正确大写,例如: "martin ye" -> "Martin Ye" ,在这种情况下,您需要遍历 String 输入以确保 String 的第一个字符和字符在一个空格大写之后。
For alphabetizing a List, I suggest storing all inputted names to an ArrayList or some other Collections object, creating a Comparator that implements Comparator, and passing that to Collections.sort()... see this questionon Comparable vs Comparator.
对于按字母顺序排列列表,我建议将所有输入的名称存储到 ArrayList 或其他一些 Collections 对象,创建一个实现 Comparator 的 Comparator,并将其传递给 Collections.sort()...参见Comparable vs Comparator 上的这个问题。
回答by elixir
This should fix it
这应该解决它
List<String> nameList = new ArrayList<String>();
nameList.add(titleCase("john smith"));
nameList.add(titleCase("tom cruise"));
nameList.add(titleCase("johnsmith"));
Collections.sort(nameList);
for (String name : nameList) {
System.out.println("name=" + name);
}
public static String titleCase(String realName) {
String space = " ";
String[] names = realName.split(space);
StringBuilder b = new StringBuilder();
for (String name : names) {
if (name == null || name.isEmpty()) {
b.append(space);
continue;
}
b.append(name.substring(0, 1).toUpperCase())
.append(name.substring(1).toLowerCase())
.append(space);
}
return b.toString();
}
回答by MadProgrammer
This can be achieved in any number of ways, most of which will come down to the details of the requirements.
这可以通过多种方式实现,其中大部分将归结为需求的细节。
But the basic premise is the same. String
is immutable (it's contents can not be changed), so you need away to extract the characters of the String
, convert the first character to upper case and reconstitute a new String
from the char
array.
但基本前提是一样的。 String
是不可变的(它的内容不能更改),因此您需要提取 的字符String
,将第一个字符转换为大写并String
从char
数组中重新构建一个新字符。
As has already been pointed out, this is relative simple.
正如已经指出的那样,这相对简单。
The other thing you might need to do, is handle multiple names (first, last) in a single pass. Again, this is relatively simple. The difficult part is when you might need to split a string on multiple conditions, then you'll need to resort to a regular expression.
您可能需要做的另一件事是在一次传递中处理多个名称(名字、姓氏)。同样,这相对简单。困难的部分是当您可能需要在多个条件下拆分字符串时,您将需要求助于正则表达式。
Here's a very simple example.
这是一个非常简单的例子。
String name = "this is a test";
String[] parts = name.split(" ");
StringBuilder sb = new StringBuilder(64);
for (String part : parts) {
char[] chars = part.toLowerCase().toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
sb.append(new String(chars)).append(" ");
}
name = sb.toString().trim();
System.out.println(name);
回答by Paul Bellora
String
has a method toCharArray
that returns a newly allocated char[]
of its characters. Remember that while String
s are immutable, elements of arrays can be reassigned.
String
有一个方法toCharArray
可以返回一个新分配char[]
的字符。请记住,虽然String
s 是不可变的,但可以重新分配数组元素。
Similarly, String
has a constructorthat takes a char[]
representing the characters of the newly created String
.
同样,String
有一个构造函数,它接受一个char[]
代表新创建的字符的String
。
So combining these, you have one way to get from a String
to a char[]
, modify the char[]
, and back to a new String
.
所以结合这些,你有一种方法可以从 aString
到 a char[]
,修改char[]
,然后回到新的String
.