Results 1 to 6 of 6

Thread: [RESOLVED] Need help with removing multiple carriage returns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Resolved [RESOLVED] Need help with removing multiple carriage returns

    Hello, I'm trying to remove multiple carriage returns and linefeeds from a string. However my code can't do this, unlikely it could, in my other application. Here's my code:

    Code:
    str = str.Replace(ControlChars.Lf, ControlChars.NewLine)
    str = str.Replace(ControlChars.Cr, ControlChars.NewLine)
    str = str.Replace(ControlChars.CrLf, ControlChars.NewLine)
    str = str.Replace(ControlChars.NewLine & ControlChars.NewLine & ControlChars.NewLine, ControlChars.NewLine)
    str = str.Replace(ControlChars.NewLine & ControlChars.NewLine, ControlChars.NewLine)

  2. #2
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Need help with removing multiple carriage returns

    Here's my example. I'm not 100% sure what a carriage return is or newline feed I just took a guess.

    Code:
    Imports System.Text.RegularExpressions
    Module Module1
        Sub Main()
    
            Dim str As String = "This is a cool \n line feed and carriage \r returnerd that returns my \r bone"
            Console.WriteLine(RemoveExcess(str))
            Console.ReadLine()
        End Sub
    
        Private Function RemoveExcess(input As String) As String
            Return Regex.Replace(input, "\\r|\\n", String.Empty)
        End Function
    End Module
    If you find my contributions helpful then rate them.

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Need help with removing multiple carriage returns

    You can do this with regex

    Code:
        Sub Main()
    
            Dim sample = <sample>
                             this
    
    is 
    
    a bunch
    
    
    
    
    of text
    
    neat!
    
    
    
                         </sample>
    
    
    
            Dim rx = New System.Text.RegularExpressions.Regex("[\r\n]+")
            Dim cleanString = rx.Replace(sample.Value, Environment.NewLine)
    
            Console.WriteLine(cleanString)
    
        End Sub
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  4. #4
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Need help with removing multiple carriage returns

    Quote Originally Posted by wild_bill View Post
    -
    I beat you again to the Regex war . This time by 3 minutes.
    If you find my contributions helpful then rate them.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Need help with removing multiple carriage returns

    Quote Originally Posted by nikel View Post
    Hello, I'm trying to remove multiple carriage returns and linefeeds from a string. However my code can't do this, unlikely it could, in my other application. Here's my code:

    Code:
    str = str.Replace(ControlChars.Lf, ControlChars.NewLine)
    str = str.Replace(ControlChars.Cr, ControlChars.NewLine)
    str = str.Replace(ControlChars.CrLf, ControlChars.NewLine)
    str = str.Replace(ControlChars.NewLine & ControlChars.NewLine & ControlChars.NewLine, ControlChars.NewLine)
    str = str.Replace(ControlChars.NewLine & ControlChars.NewLine, ControlChars.NewLine)
    Are you trying to replace multiple CR / LF with a single NewLine or replace all CR / LF with some other char?
    This will replace multiples with a single:
    Code:
            Dim Sentence As String
            Sentence = "Line1" & ControlChars.Lf & Environment.NewLine & ControlChars.CrLf & ControlChars.Cr & "line2"
    
            Debug.WriteLine(Sentence)
            Sentence = String.Join(Environment.NewLine, Sentence.Split(New Char() {ControlChars.Lf, ControlChars.Cr}, StringSplitOptions.RemoveEmptyEntries))
    
            Debug.WriteLine("")
            Debug.WriteLine(Sentence)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2013
    Posts
    200

    Re: Need help with removing multiple carriage returns

    Thanks for all replies. dbasnett: Your code works fine. But why the others doesn't?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width