从 imageJ 中拆分图像名称后,我试图 select 字符串的最后一个索引。我知道在 python 中我们可以做 string[-1]

I am trying to select the last index of a string after splitting an image name from imageJ. I know in python we can do string[-1]

imageName=getTitle(); #This returns 一个包含图像所在位置的完整路径的字符串。

image1 = split(imageName,"/"); #splits 图像,其中“/”是。根据路径,长度不同

图片=图片1[1]; #我想要最后一个,但它不会总是第n个索引。

split() returns 一个数组。在您的示例中,您将数组分配给 image1,因此您可以使用 image1.length 找到数组的长度。 ImageJ macro 语言中的数组是从 0 开始的,因此您需要减去 1 才能找到最后一个元素。

imageName=getTitle(); #This returns a string with the entire path of where the image is.
image1 = split(imageName,"/"); #splits the image where "/" is. Based on path, this varies in length
image = image1[image1.length - 1]; #last element.

请注意,使用 / 拆分将适用于 mac 但不适用于 Windows。

我的回答是针对你的索引问题。如果您只是想要没有路径的文件名,请查看 File.nameFile.nameWithoutExtension 以获取更直接的方法来获取您需要的内容。

你写: "imageName=getTitle(); #This returns 一个包含图像所在位置的完整路径的字符串。"

这不是真的。 “获取标题();” returns最前面的图像的名称,包括其后缀,而不是路径!