跳转到内容

slice

string  slice(string s, int start, int end)

string  slice(string s, int start, int end, int step)

从较长的字符串中提取子字符串。

<type>[] slice(<type>s[], int start, int end)

<type>[] slice(<type>s[], int start, int end, int step)

从较大的数组中提取子数组。

string  slice(string s, int hasstart, int start, int hasend, int end, int hasstep, int step)

<type>[] slice(<type>array[], int hasstart, int start, int hasend, int end, int hasstep, int step)

支持切片语法的通用签名。如果hasstart0,则忽略start并使用0;如果hasend0则忽略end并使用数组长度;如果hasstep0则忽略step并使用1

  • 此函数等效于使用value[start:end:step]切片语法。
  • 如果startend为负数,则从字符串/数组末尾开始计数。
  • 计算得到的范围会被限制在原始字符串/数组的边界内。
  • 如果步长(step)为零,函数返回空字符串/数组。
  • 如果步长为负数,则按相反顺序返回元素,此时end应小于start

示例

int[] nums = {10, 20, 30, 40, 50, 60};
slice(nums, 1, 3) == {20, 30}; // nums[1:3]
slice(nums, 1, -1) == {20, 30, 30, 40, 50}; // nums[1:-1]
slice(nums, 0, len(nums), 2) == {20, 40, 60}; // nums[0:len(nums):2]
slice(nums, 0, 0, 0, 0, 1, 2) == {20, 40, 60}; // nums[::2]