首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

获取拆分字符串数组的最后一个元素

在编程中,我们经常需要获取拆分字符串数组的最后一个元素。以下是一些常见编程语言的代码示例:

  1. Python:
代码语言:python
代码运行次数:0
复制
string = "hello.world"
split_string = string.split(".")
last_element = split_string[-1]
print(last_element)
  1. JavaScript:
代码语言:javascript
复制
const string = "hello.world";
const splitString = string.split(".");
const lastElement = splitString[splitString.length - 1];
console.log(lastElement);
  1. Java:
代码语言:java
复制
String string = "hello.world";
String[] splitString = string.split("\\.");
String lastElement = splitString[splitString.length - 1];
System.out.println(lastElement);
  1. C#:
代码语言:csharp
复制
string s = "hello.world";
string[] splitString = s.Split('.');
string lastElement = splitString[splitString.Length - 1];
Console.WriteLine(lastElement);

在这些示例中,我们首先使用 split 函数将字符串拆分为数组,然后使用数组的长度减去 1 作为索引来获取最后一个元素。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 领券