how can I remove 4 character of a string?

10 visualizaciones (últimos 30 días)
sara adam
sara adam el 20 de Nov. de 2018
Comentada: Jan el 30 de Ag. de 2021
for example string is LASTNAME ,output=NAME

Respuestas (2)

KALYAN ACHARJYA
KALYAN ACHARJYA el 20 de Nov. de 2018
Editada: KALYAN ACHARJYA el 20 de Nov. de 2018
s='lastname';
modified_s=s(5:end);
%Command Window
>> s='lastname'
>> modified_s=s(5:end)
s=lastname
modified_s=name
  1 comentario
Jan
Jan el 20 de Nov. de 2018
s(5:end) can be interpreted as: Keep the last part. This is the same as removing the leading 4 characters:
s = 'lastname';
s(1:4) = [];

Iniciar sesión para comentar.


John Cunningham
John Cunningham el 26 de Ag. de 2021
If s needs to be a string, just convert to character, grab the indices you need, then convert back to string.
K>> s = "lastname";
K>> s = char(s);
K>> s = string(s(5:end))
s =
"name"
  2 comentarios
Stephen23
Stephen23 el 27 de Ag. de 2021
Editada: Stephen23 el 27 de Ag. de 2021
Simpler and more efficient to use indexing to access the character vector that is already inside the string container:
s = "lastname";
s{1} = s{1}(5:end)
s = "name"
No CHAR call, no STRING call required.
Jan
Jan el 30 de Ag. de 2021
Or:
s = "lastname";
t = extractAfter(s, 4)

Iniciar sesión para comentar.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by