java 字符串的第一个和最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26693220/
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
First and Last Character of a String
提问by Certus
This lab we are doing in class is giving me a lot of trouble, and I wanted someone to help explain it to me. My teacher is out of town this weekend and so he can't answer my questions. The point of this lab is to find the first and last letter of each string. We also have to use "return" methods, but I am confused as to how they should be used. I know that I have to use "charAt();", but i'm not sure where to put this and how to test certain strings from another method.
我们在课堂上做的这个实验给我带来了很多麻烦,我想有人帮我解释一下。我的老师这个周末不在城里,所以他不能回答我的问题。本实验的重点是找出每个字符串的第一个和最后一个字母。我们还必须使用“返回”方法,但我对如何使用它们感到困惑。我知道我必须使用“charAt();”,但我不确定把它放在哪里以及如何从另一种方法测试某些字符串。
import static java.lang.System.*;
public class firstandlast
{
public String s, word, getFirst, getLast;
public firstandlast(String s)
{
}
public void setString(String s)
{
}
public String getFirst()
{
return "";
}
public String getLast()
{
return "";
}
public String toString()
{
String output = "";
return output;
}
}
Code that tests the main class:
测试主类的代码:
import static java.lang.System.*;
public class firstrunner
{
public static void main ( String[] args )
{
firstandlast demo = new firstandlast("Hello");
demo.setString("Hello");
System.out.println( "first letter :: " + demo.getFirst() );
System.out.println( "last letter :: " + demo.getLast() );
demo.setString("World");
System.out.println( "first letter :: " + demo.getFirst() );
System.out.println( "last letter :: " + demo.getLast() );
//add more test cases
}
}
I appreciate any help!
我感谢任何帮助!
回答by furkle
I'll give you a couple hints:
我会给你一些提示:
charAt is a method belonging to objects of type String, meaning that the dot and the method name go after the variable name (e.g.
yourString.extensionMethod()
)When you use charAt, you're basically treating the string like an array of char objects. What would you do to get the first element in an array? What would you do to get the last?
charAt 是属于 String 类型对象的方法,这意味着点和方法名称在变量名称之后(例如
yourString.extensionMethod()
)当您使用 charAt 时,您基本上将字符串视为一个 char 对象数组。你会怎么做才能得到数组中的第一个元素?你会怎么做才能得到最后一个?
There are plenty of pages on StackOverflow that'll tell you exactly how to do this.
StackOverflow 上有很多页面会准确地告诉您如何执行此操作。
回答by AsSiDe
Try This:
试试这个:
public class FirstAndLast {
private String message;
public FirstAndLast(String message) {
this.message=message;
}
public char getFirst(){
return message.charAt(0);
}
public char getLast(){
int len=message.length();
return message.charAt(len-1);
}
public static void main(String[] args) {
FirstAndLast fl=new FirstAndLast("Here you go");
System.out.println("First Char is "+fl.getFirst());
System.out.println("First Char is "+fl.getLast());
}
}
回答by Misfits FTW
Scanner s=new Scanner(System.in);
System.out.println("Enter a word longer than 2 characters: ");
String word=s.next();
char a = word.charAt(0);
System.out.println(a);
int lastNum = word.length();
int x=lastNum-1;
char z = word.charAt(x);
System.out.println(z);