我是Java的新手。
我使用以下代码从用户
获取输入 Class Test
{
public static void main(String arg[])
{
Scanner in=new Scanner(System.in);
String s=in.nextLine();
System.out.println(s);
in.close();
}
}
当用户输入:
"HI"
我想打印为:
HI
(没有双引号) 请帮忙......
答案 0 :(得分:0)
您可以使用String x = s.replaceAll("\"", "");
删除双引号。
答案 1 :(得分:0)
If you use a solution with String.replace*
you will be replacing all double quotes, even if they are not the first and last characters.
If you want to make sure to only remove the leading and trailing double quotes, you can make sure they are there and then remove them:
if (original.startsWith("\"") && original.endsWith("\"")) {
String newString = original.substring(1, original.length() - 1);
}