How to Remove Line Break in String

前端 未结 13 2093
旧时难觅i
旧时难觅i 2020-12-09 01:16

I want to Remove the Line Break from the string if my string Ends with Line Break.

Sub linebreak(myString)
    If Len(myString) <> 0 Then
        If Rig         


        
相关标签:
13条回答
  • 2020-12-09 01:50

    No one has ever suggested a RegExp solution. So here is one:

    Function TrimTrailingLineBreak(pText)
        Dim oRE: Set oRE = New RegExp: oRE.Global = True
        oRE.Pattern = "(.*?)(\n|(\r\n)){1}$"
        TrimTrailingLineBreak = oRE.Replace(pText, "$1")
    End Function
    

    It captures and returns everything up until a single ({1}) trailing new line (\n), or carriage return & new line (\r\n), at the end of the text ($).
    To remove all trailing line breaks change {1} to *.
    And to remove all trailing whitespace (including line breaks) use oRE.Pattern = "(.*?)\s*$".

    0 讨论(0)
提交回复
热议问题