I too want acknowledge all your work to explain why things work the way they do. But I want also say that I did read the docs before asking. It just happened that I missed that first part on the "Strings" that talk about strings immutability and error message does not help much leading us to the relevant manual section (I didn't go down the 'bytestrings', 'ascii' functions I missed the 'replace' one). Furthermore, this really should be mentioned to in the "Noteworthy differences from MATLAB" section as I'm sure others (Matlabers) will fall in the same hole. > > Getting back to your original example, I would counter that you never > really want to just do something like "replace the 16th character of this > string" in isolation. How did you figure out the index 16? What you really > want to do here is replace every instance of the NUL byte with a space. The > replace function will do that for you: >
Yes, I wanted to remove the '\0' that I happened to know where it was because the first part of the string (obtained by concatenation) came from a ccall that gives back a Cstring (I actually solved the problem by not writing the last char of string one at the concatenation step) BTW, why doesn't the @sprint remove the NUL byte when we do julia> s1 = "ABC\0" "ABC\0" julia> @sprintf("%s Bla bla",s1) "ABC\0 Bla bla" > > julia> s = "@GMTAPI@-000000\0 -R-10/0/35/45 -JM14c -Ba2 -P > lixo.ps" > "@GMTAPI@-000000\0 -R-10/0/35/45 -JM14c -Ba2 -P > lixo.ps" > > julia> replace(s, '\0', ' ') > "@GMTAPI@-000000 -R-10/0/35/45 -JM14c -Ba2 -P > lixo.ps" > > > Only want to replace one NUL byte? Just use replace(s, '\0', ' ', 1) > instead. Want to replace each NUL byte and any trailing whitespace with a > single space? That's easy too: > > julia> replace(s, r"\0\s*", ' ') > "@GMTAPI@-000000 -R-10/0/35/45 -JM14c -Ba2 -P > lixo.ps" > > > So the fact that strings are immutable is compensated for by providing > much more powerful tooling to manipulate string values. >