indexingimagejfiji

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 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[1]; #I want the last one but it will not always be the nth index.


Solution

  • split() returns an array. In your example you assign the array to image1 so you can find the length of the array using image1.length. Arrays in ImageJ macro language are 0-based so you need to subtract 1 to find the last element.

    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.
    

    Note that splitting using / will work on mac but not Windows.

    My answer is directed at your indexing question. If you simply want the file name without the path, Take a look at File.name and File.nameWithoutExtension for more straightforward ways to get what you need.