这是一个测试:
console.log(" Golden Katana of the Unflinching Dawn ".replace(/\s+/g,""))
Run Code Online (Sandbox Code Playgroud)
我想删除字符串末尾的多余空格,但它会删除字符串中的每个空格,所以我怎样才能删除末尾的额外空格而只是保留Golden Katana of the Unflinching Dawn?
public String withoutEnding(String str){
return (boolean)(str.length() <= 2) && "" || str.substring(1,str.length() - 1);
}
Run Code Online (Sandbox Code Playgroud)
而不是
public String withoutEnding(String str){
if (str.length <= 2)
return "";
else return str.substring(1,str.length() - 1)
}
Run Code Online (Sandbox Code Playgroud)
它会出错,第一个.我在我的AP计算机科学课上开始学习Java,虽然我对编码有一些了解,但这让我感到烦恼,我不知道如何在Google上搜索我的问题,如果可以在另一篇文章中找到,那就很抱歉.
但只是想知道为什么第一个不起作用.