迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

如何在 Java 中删除字符串中的最后一个字符

作者:迹忆客 最近更新:2023/03/28 浏览次数:

本文介绍了如何在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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()方法提取年份和月份。

如何获取 Pandas DataFrame 的行数

发布时间:2024/04/23 浏览次数:71 分类:Python

本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便