Extract substring from the last position in Matlab

2019-09-22 01:56发布

问题:

I have got this one:

string = 'abcdefgh'
string(1:3) = 'abc'

Is this possible string(-6:1) = 'abcdef' .

I know this is not possible , what is the closest that is possible ; something which is similar to Python substring syntax.

Edit : I want to get rid of .jpgand C:\ from C:/hello.jpgto get just hello . Using strsplit twice is just cumbersome . I just know the length of C:\ and .jpg, but not of the whole string.

回答1:

Use the end statement. This will point to the last character in the vector/matrix/string/whatever. So, for instance

string(2:end-1) -> 'bcdefg';
string(end-3:end) -> 'efgh';