If I have a string consisting of words of different lengths separated by spaces, how can return a word in that string given only its starting index?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Adoniram
el 3 de En. de 2019
Comentada: Adoniram
el 4 de En. de 2019
As an example:
str = 'bat catch convey can cart court CUT ct CAT-scan';
...and all I have is my starting index, which is 11; consequently I want to return the word "convey" from this string. How do I proceed?
I find that "extractBetween" forces you to choose either between a start string and an end string, i.e.
newStr = extractBetween(str,startStr,endStr)
or a start position and an end position, i.e.
newStr = extractBetween(str,startPos,endPos)
Many thanks for whatever illumination you can provide.
0 comentarios
Respuesta aceptada
Image Analyst
el 4 de En. de 2019
Here's how I thought of to do it:
str = 'bat catch convey can cart court CUT ct CAT-scan';
remainingWords = strsplit(str(11:end)) % Get words starting with index 11.
theWord = remainingWords{1} % Take the first word it found.
Más respuestas (1)
Walter Roberson
el 4 de En. de 2019
Editada: Walter Roberson
el 4 de En. de 2019
starting_index = 11; %given
mask = str == ' ';
starts = strfind([1 mask], [1 0]);
stops = strfind([mask 1], [0 1]);
wordidx = find(starts == starting_index);
if isempty(wordidx)
error('That starting index is not the beginning of a word')
end
the_word = str(starting_index : stops(wordidx));
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!