简体   繁体   English

用户使用子字符串(Java)输入的字符串的最后两个字母

[英]Last two letters of a user inputted string using substring (Java)

I'm writing a program that generates star wars names. 我正在编写一个生成星球大战名字的程序。 The user inputs their first, last, mother's maiden name, birth city, and first car and the program gives a star wars name. 用户输入他们的名字,姓氏,母亲的娘家姓,出生城市和第一辆汽车,然后程序给出一个星球大战名称。 I need the last two characters* of the user inputted last name. 我需要输入用户姓氏的最后两个字符*。 I know I can use substring , but I cannot get anything to work. 我知道我可以使用substring ,但是我什么都无法工作。 The best I have is: 我最好的是:

lastname.substring(lastname.length() -2)

which gives the first two letters of the last name, and not the last. 给出姓氏的前两个字母,而不是姓氏。 Also, I cannot use lastname.substr(-2) because substr for some reason won't work (not sure why, this would be much easier). 另外,我不能使用lastname.substr(-2)因为由于某种原因, substr无法工作(不知道为什么,这会容易得多)。

Thanks for any help. 谢谢你的帮助。

*EDIT: I hope I didn't confuse anyone, I need the last two characters of the last name. *编辑:我希望我不要混淆任何人,我需要姓氏的最后两个字符。

Actually I see my problem now: my original last name variable is 实际上,我现在看到了我的问题:我原来的姓氏变量是

String lastname = console.nextLine().substring(0,2).trim().toUpperCase();

which keeps the first two letters, so the reason I was getting the first two letters was because of this. 它保留了前两个字母,所以我得到前两个字母的原因是因为这个。 I understand now. 我现在知道了。 Is there a way to get the last two letters from this same variable? 有没有办法从同一个变量中获取最后两个字母?

So if the name was Michael , you just want Micha ? 所以,如果名字叫Michael ,您只想要Micha吗?

Try: 尝试:

String trimmedLastName = lastName.substring(0, lastName.length() - 2);

For example: 例如:

String lastName = "Michael";
String trimmedLastName = lastName.substring(0, lastName.length() - 2);
System.out.println(trimmedLastName); // prints Micha

The reason mine works and yours doesn't is because the first parameter of substring is where to start . 我的工作而您的工作不起作用的原因是, substring的第一个参数是从哪里开始 So I start from the first letter, and end on the second last letter (not inclusive). 因此,我从第一个字母开始,到第二个最后一个字母(不包含)结束。

What yours does is start on the second last letter, and continue on until the end of the string. 您要做的是从倒数第二个字母开始,一直到字符串结尾。


However, if you wanted just el from Michael , you could do this: 但是,如果您只想要Michael el ,可以这样做:

String lastName = "Michael";
String trimmedLastName = lastName.substring(lastName.length() - 2);
System.out.println(trimmedLastName); // prints el

试试看

lastname.substring(lastname.length() -3,lastname.length() -1);

If you need the different ways, here: 如果您需要其他方式,请在此处:

StringBuffer myString = new StringBuffer(inputString);
myString.revert();

StringBuffer userInput = new StringBuffer(myString.subString(2));

String result = userInput.revert();

Have a nice day. 祝你今天愉快。

lastname.substring(lastname.length() - 2) should return the last 2 letters. lastname.substring(lastname.length() - 2)应该返回最后2个字母。

lastname.substring(0, 2) returns the first two. lastname.substring(0, 2)返回前两个。

substring(index) is includive substring(index)包含在内

substring(index1, index2) is inclusive, exclusive respectively. substring(index1, index2)分别是包含式和排除式的。

Because String is immutable so when you call subString it doesn't change the value of lastname. 因为字符串是不可变的,所以当您调用subString时,它不会更改姓氏的值。

I think you should do: String genName = lastname.subString(lastname.lengh()-2); 我认为您应该这样做:String genName = lastname.subString(lastname.lengh()-2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM