Android Code Snippet Remove linefeeds & carriagereturn/linefeeds from strings

Discussion in 'Code Snippets' started by gravel, Jul 15, 2019.

  1. gravel

    gravel Member Licensed User

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

    Code:
    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.
     
    asales likes this.
  2. sorex

    sorex Expert Licensed User

    isn't this working aswell without the need for the extra javaobject library?

    Code:
    return Regex.Replace("[/\r?\n|\r/]",TextToClean," "))
     
  3. gravel

    gravel Member Licensed User

    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
     
    emexes likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice