Java 中可以使用 charAt() 方法来获取字符串的首字符,使用 substring() 方法来去除字符串的首字符。

获取字符串首字符的方法如下:

String str = 'Hello World';
char firstChar = str.charAt(0);
System.out.println(firstChar); // 输出:H

去除字符串首字符的方法如下:

String str = 'Hello World';
String newStr = str.substring(1);
System.out.println(newStr); // 输出:ello World

注意:字符串的索引是从 0 开始的,所以获取首字符时索引为 0,去除首字符时需要将索引从 1 开始。

Java 字符串操作:获取首字符和去除首字符

原文地址: https://www.cveoy.top/t/topic/qmYH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录