Borrar filtros
Borrar filtros

How to erase newline character from string?

316 visualizaciones (últimos 30 días)
Caglar
Caglar el 12 de Nov. de 2018
Editada: Alexander el 18 de Jun. de 2024
Hello,
I am reading an xml file that contains new line (enter) character. How do I detect and erase it?
text=a.Olay(1)
text =
"YBasla
"
As you can see there is a new line character at the end of the string. Erase could not delete it this way:
erase(text,'↵')
ans =
"YBasla
"
Note that the string looks like this in a table
a =
6×5 table
(...) Olay (...) (...) (...)
_________________ ____________________________ ______________________ _________ __________
(...) "YBasla↵" (...) (...) (...)
I uploaded table and the text variable so that you can play with them.
  2 comentarios
Walter Roberson
Walter Roberson el 12 de Nov. de 2018
erase(text,char(10))
Question though: is it possible that there are carriage returns as well as newline?
Alexander
Alexander el 18 de Jun. de 2024
Editada: Alexander el 18 de Jun. de 2024

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 12 de Nov. de 2018
strtrim(text)
  3 comentarios
Stephen23
Stephen23 el 12 de Nov. de 2018
Editada: Stephen23 el 12 de Nov. de 2018
"...but new line character is not always at the end or beginning, sometimes there are a few in one string."
regexprep(text,'\n+','')
or if you want to include carriage return as well (if you use Windows):
regexprep(text,'[\n\r]+','')
Donavan Haidinger
Donavan Haidinger el 8 de Feb. de 2021
When working with the replace function, you might need to use char(10) or newline rather than '\n+'
Example:
table.fieldname = replace(table.fieldname,char(10),' ')
or
table.fieldname = replace(table.fieldname,newline,' ')
Both answers yield the same result

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by