如何在 Java 中删除字符串中的最后一个字符
本文介绍了如何在 Java 中删除字符串中的最后一个字符,还列举了一些示例代码来理解这个主题。删除最后一个字符的方法有好几种,比如 substring()
方法、StringUtils
类等。让我们来仔细看看这些例子。
使用 Java 中的 substring()
方法从字符串中删除最后一个字符
String
类的 substring()
方法用于从字符串对象中获取子字符串。它需要两个参数:起始索引和最后索引。在这里,我们使用 substring()
方法在排除最后一个字符后得到一个字符串。请看下面的例子。
public class SimpleTesting
{
public static void main(String args[])
{
String str = "removea";
str = str.substring(0, str.length()-1);
System.out.println(str);
}
}
输出:
remove
在 Java 中使用 replaceFirst()
方法从字符串中删除最后一个字符
我们可以使用 replaceFirst()
方法从字符串中删除最后一个字符。它在替换了这个字符后返回一个字符串。请看下面的例子。
public class SimpleTesting
{
public static void main(String args[])
{
String str = "removea";
str = str.replaceFirst(".$","");
System.out.println(str);
}
}
输出:
remove
使用 Java 中的 removeEnd()
方法从字符串中删除最后一个字符
如果你正在使用 Apache commons lang
库,最好使用 StringUtils
类,它提供 removeEnd()
方法来删除字符串的最后一个字符。请看下面的例子。
import org.apache.commons.lang3.StringUtils;
public class SimpleTesting
{
public static void main(String args[])
{
String str = "removea";
str = StringUtils.removeEnd(str, "a");
System.out.println(str);
}
}
输出:
remove
使用 Java 中的 chop()
方法从字符串中删除最后一个字符
另一个方法,StringUtils
类的 chop()
,从指定的字符串中删除最后一个字符后返回一个字符串。
import org.apache.commons.lang3.StringUtils;
public class SimpleTesting
{
public static void main(String args[])
{
String str = "removea";
str = StringUtils.chop(str);
System.out.println(str);
}
}
输出:
remove
使用 Java 中的 substring()
方法从字符串中删除最后一个字符
StringUtils
类提供了 substring()
方法,该方法需要三个参数。第一个是一个字符串,第二个是一个起始索引,第三个是字符串的结束索引。它返回一个字符串,也就是指定字符串的子字符串。请看下面的例子。
import org.apache.commons.lang3.StringUtils;
public class SimpleTesting
{
public static void main(String args[])
{
String str = "removea";
str = StringUtils.substring(str, 0, -1);
System.out.println(str);
}
}
输出:
remove
在 Java 8 或更高版本中删除字符串中的最后一个字符
如果你使用的是更高版本的 Java,你可以使用 Optional
类来避免 null 异常,并采用函数方法从字符串中删除最后一个字符。
import java.util.Optional;
public class SimpleTesting
{
public static void main(String args[])
{
String str = "removea";
str = Optional.ofNullable(str)
.filter(s -> s.length() != 0)
.map(s -> s.substring(0, s.length() - 1))
.orElse(str);
System.out.println(str);
}
}
输出:
remove
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何从 Pandas 的日期时间列中提取月份和年份
发布时间:2024/04/23 浏览次数:160 分类:Python
-
我们可以分别使用 dt.year()和 dt.month()方法从 Datetime 列中提取出年和蛾。我们还可以使用 pandas.DatetimeIndex.month 以及 pandas.DatetimeIndex.year 和 strftime()方法提取年份和月份。
如何检查 NaN 是否存在于 Pandas DataFrame 中
发布时间:2024/04/23 浏览次数:208 分类:Python
-
我们可以使用 isnull()和 isna()方法检查 Pandas DataFrame 中是否存在 NaN。
如何在 Pandas DataFrame 的列中将所有 NaN 值替换为零
发布时间:2024/04/23 浏览次数:198 分类:Python
-
在 Pandas 库中使用 df.fillna(),df.replace()方法在 DataFrame 中将 NaN 值替换为零
如何在 Pandas 中更改列的数据类型
发布时间:2024/04/23 浏览次数:183 分类:Python
-
本教程介绍了如何通过使用 to_numaric,as_type 和 infer 对象来更改 Pandas 中列的数据类型。
如何对 Pandas 中的 DataFrame 行随机排序
发布时间:2024/04/23 浏览次数:128 分类:Python
-
我们可以使用 sample(),shuffle()和 permutation()方法随机地对 Pandas 中的 DataFrame 行进行随机排序。
如何获取 Pandas DataFrame 的行数
发布时间:2024/04/23 浏览次数:71 分类:Python
-
本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。