在 Java 中获取字符串的最后一个字符

本教程将介绍我们如何在 Java 中获取字符串的最后一个字符。

在 Java 中使用 substring() 获取字符串的最后一个字符

在下面的示例代码中,exampleString 的最后一个字符是 g。我们将使用 String 类的一个名为 substring() 的方法,正如它的名字所示,它将取出一个子字符串。substring(startIndex) 方法接受一个参数,这个参数应该是我们需要的字符串部分的索引或位置。我们只需要最后一个字符,所以我们传递 exampleString.length() - 1 作为起始索引。

现在我们在 lastCharacter 变量中得到了最后一个字符,但它是字符串类型。我们需要将我们的字符作为一个 char,这样我们就可以使用 toCharArray()lastCharacter 转换为一个 char 的数组,然后得到该数组中唯一的一个字符。

public class LastCharString {
    public static void main(String[] args) {
        String exampleString = "This is a String";
        String lastCharacter = exampleString.substring(exampleString.length() - 1);
        char[] lastChar = lastCharacter.toCharArray();
        System.out.println("Last char: "+lastChar[lastChar.length - 1]);
    }
}

输出:

Last char: g

在 Java 中使用 charAt() 获取一个字符串的最后一个字符

我们可以通过使用 String 类的 chartAt() 方法直接获取字符串中的最后一个字符,而不是以字符串的形式获取最后一个字符,然后将其转换为 char[]。这个方法可以让我们在指定的索引号处得到一个特定的单个字符。

exampleString.charAt(exampleString.length() - 1) 就是我们获取位于 exampleString 最后位置的字符。

public class LastCharString {
    public static void main(String[] args) {
        String exampleString = "This is a String";
        char lastChar = exampleString.charAt(exampleString.length() - 1);
        System.out.println("Last char: " + lastChar);
    }
}

输出:

Last char: g

在 Java 中通过将字符串转换为 Char 数组来获取字符串的最后一个字符

我们可以使用的另一个简短的方法是直接将 exampleString 转换成一个 char 的数组。这使得代码更加简洁,速度更快。我们可以使用本文第一个例子中使用的 toCharArray() 方法,将整个字符串转换为一个 char 数组。然后我们通过访问数组中最后一个字符的索引-lastCharArray.length - 1 来获取最后一个字符。

public class LastCharString {
    public static void main(String[] args) {
        String exampleString = "This is a String";
        char[] lastCharArray = exampleString.toCharArray();
        char lastChar = lastCharArray[lastCharArray.length - 1];
        System.out.println("Last char: " + lastChar);
    }
}

输出:

Last char: g

在 JavaScript 中获取字符串的最后一个字符

在我们继续之前,让我们知道 JavaScript 如何理解 string 对象。与 C、Java 等其他编程语言不同,javascript 将 string 视为字符数组。因此,我们可以对字符串执行数组操作以获取其最后一个字符。我们可以使用以下 javascript 内置函数来实现这一目的。

使用 JavaScript 的 substr(-1) 函数提取字符串的最后一个字符

substr() 类似于最常用的函数 substring()。我们可以使用这些函数中的任何一个来根据参数值获取字符串的一部分。substr() 接受两个参数,一个是 index(从 0 开始),我们需要从中获取子字符串和子字符串的长度。为了获取字符串的最后一个字符,我们可以将 -1 作为参数传递给 substr() 函数,如下面的代码片段所示。

var a = "hello";
console.log(a.substr(-1));

输出:

o

我们还可以使用 substring() 函数来获取字符串的最后一个字符。它需要两个参数,开始索引和结束索引。因此,为了从字符串对象中获取最后一个字母,我们将起始索引定义为 string length - 1,将结束索引定义为字符串的长度。

var a = "hello";
console.log(a.substring(a.length-1, a.length));

输出:

o

使用 charAt 函数获取字符串的最后一个字符

charAt() 函数返回字符串指定位置的 字符。它将预期字符的位置作为参数。因此,为了获取字符串的最后一个字符,我们可以将字符串的 length - 1(因为字符串索引从 0 开始)作为参数传递给它。参考以下用法。

var a = "hello";
console.log(a.charAt(a.length - 1));

输出:

o

使用 slice(-1) 函数获取最后一个字符

slice() 函数也是一种常用的对字符串进行操作的方法。对于它所接受的参数,它类似于 substring() 函数。这两个函数都接受两个参数,开始索引和结束索引。它们的行为不同,因为 slice(-1) 给出了字符串的最后一个字符。因此,它类似于 substr(),用于获取字符串的最后一个字符。

var a = "hello";
console.log(a.slice(-1));

输出:

o

获取字符串的最后一个索引

我们也可以使用常规方法在 javascript 中获取字符串的最后一个字符。由于 javascript 将字符串对象视为字符数组,因此我们可以使用 string[length - 1] 语法检索该数组的最后一个元素。它类似于在其他编程语言(如 C 和 Java)中处理字符数组。

var a = "hello";
console.log(a[a.length-1]);

输出:

o

使用 Javascipt 的 split() 函数

使用 split() 函数,我们可以将字符串分解为各种子字符串。该函数根据我们作为参数传递给它的分隔符拆分字符串。为了获取字符串的最后一个字符,我们可以使用""(空字符)作为分隔符,并单独获取数组中的所有字符。然后我们需要获取数组的最后一个元素。我们可以通过使用 array[array.length](常用的数组语法)来从数组中获取给定索引处的元素。

var a = "hello";
console.log(a.split("")[a.length - 1]);

输出:

o

总结

总结我们上面讨论的从字符串中提取最后一个字符的所有方法,我们可以将它们全部放在一个代码片段中。substr(-1)slice(-1) 是最容易使用的。

var a = "hello";
console.log(a.substr(-1));
console.log(a.substring(a.length-1, a.length));
console.log(a.charAt(a.length - 1));
console.log(a.slice(-1));
console.log(a[a.length-1]);
console.log(a.split("")[a.length - 1]);

输出:

o
o
o
o
o
o

笔记

  • 本文中讨论的任何函数都不会改变原始字符串。因此,它们可以在整个应用程序中安全使用,没有任何副作用。
  • 所有浏览器都支持所有功能,包括旧版本的 Internet Explorer。

在 JavaScript 中获取字符串的最后 N 个字符

要获取字符串的最后 N 个字符,请对字符串调用 slice 方法,传入 -n 作为参数,例如 str.slice(-3) 返回一个包含原始字符串最后 3 个字符的新字符串。

const str = 'Hello World';

const last3 = str.slice(-3); // ?️ rld
console.log(last3);

const last2 = str.slice(-2); // ?️ ld
console.log(last2);

我们传递给 String.slice 方法的参数是起始索引。

例如,传递负索引 -3 意味着给我字符串的最后 3 个字符。

这与传递 string.length - 3 作为起始索引相同。

const str = 'Hello World';

const last3 = str.slice(-3); // 👉️ rld
console.log(last3);

const last3Again = str.slice(str.length - 3); // 👉️ rld
console.log(last3Again);

在这两个示例中,我们都告诉 slice 方法将字符串的最后 3 个字符复制到一个新字符串中。

即使我们尝试获取比字符串包含的字符更多的字符,String.slice 也不会抛出错误。 相反,它将返回整个字符串的副本。

const str = 'Hello World';

const last100 = str.slice(-100); // 👉️ Hello World
console.log(last100);

在示例中,我们尝试获取仅包含 11 个字符的字符串的最后 100 个字符。

结果,slice 方法返回了整个字符串的副本。

我们还可以使用 String.substring 方法获取字符串的最后 N 个字符。

const str = 'Hello World';

// 👇️ using substring
const last3 = str.substring(str.length - 3); // 👉️ rld
console.log(last3);

但是,使用 String.slice 方法更加直观和可读。

例如,如果我们将负参数传递给 String.substring 方法,它会将其视为传递 0,这非常令人困惑。

有关 String.substring 和 String.slice 之间的其他差异,请查看 MDN 文档。