Search code examples
stringmatlabreplaceoctavenon-ascii-characters

Remove non ASCII characters in octave


I'm trying to remove non ASCII characters read from a data file using OCTAVE but I can't make it work. I tried getting the ASCII codes of these "weird" characters and they do have random ASCII codes. An example string of characters is this:

asdqwФЕДЕРАЛЬ234НОЕ234 АГЕНТСqewwqedasТВО ПasdsadО ОБРАasdasdЗОВАНИЮ Госудаsadasdsagwfрственная акадеasdмия профессиональной п

Do you guys have any suggestions on how can i remove the non ASCII characters from this string? Or better yet, how will I be able to determine if a given string has non ASCII characters?

Thanks in advance!


Solution

  • To remove all non ASCII chars in the range of 0..127 decimal use

    a = "asdqwФЕДЕРАЛЬ234НОЕ234 АГЕНТСqewwqedasТВО ПasdsadО ОБРАasdasdЗОВАНИЮ Госудаsadasdsagwfрственная акадеasdмия профессиональной п";
    a(! isascii (a)) = []
    

    which gives

    a = asdqw234234 qewwqedas asdsad asdasd sadasdsagwf asd 
    

    and if you just want to check if there are non ASCII chars:

    any (! isascii("foobar"))
    ans = 0
    any (! isascii("foobaröäüß"))
    ans =  1