Android Code Snippet Remove linefeeds & carriagereturn/linefeeds from strings

To remove lenefeeds and carriage return/line feeds from inside strings and replace them with a single space.

B4X:
    Dim jo As JavaObject = Regex.matcher("[/\r?\n|\r/]", TextToClean)
   Return jo.RunMethod("replaceAll", Array(" "))

It might be straightforward if you're familiar with regular expressions, but it took me a while to find something that worked.
 

gravel

Member
Licensed User
Longtime User
isn't this working aswell without the need for the extra javaobject library?

B4X:
return Regex.Replace("[/\r?\n|\r/]",TextToClean," "))
Yes, I think you're right. This was probably lazyness on my part. After using the similar code to remove embeded multiple spaces in strings https://www.b4x.com/android/forum/threads/remove-multiple-spaces-inside-strings.107672/, I eventually ran into a string with multiple CrLF's and my approach to dealing with it must have been 'limited' by my previous thing with the multiple spaces
 
Top