Results 1 to 38 of 38

Thread: [RESOLVED] Remove trailing NewLines from string

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Resolved [RESOLVED] Remove trailing NewLines from string

    I cant get my head around this, I have a string that may contain more than one newline at the end, and I want to trim them off, but I cant use String.Trim because the string my contain a space as the last char before the Newlines.
    thanks in advance
    Chris

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Remove trailing NewLines from string

    Use the string functions to replace the chars you want. .Replace, .IndexOf, .LastIndexOf, and .SubString are what you want to look into using depending on your actual string contents.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Remove trailing NewLines from string

    mystring = mystring.Substring(0, mystring.lastindexof(" "))

    ?
    I don't live here any more.

  4. #4

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    Quote Originally Posted by wossname
    mystring = mystring.Substring(0, mystring.lastindexof(" "))

    ?
    no that doesnt work

    VB Code:
    1. Data = "abcdefghijkl" & vbNewLine & vbNewLine & vbNewLine
    2. Data = Data.Substring(0, Data.LastIndexOf(" "))
    3. MessageBox.Show(Data)
    gives exception

    Quote Originally Posted by RobDog888
    Use the string functions to replace the chars you want. .Replace, .IndexOf, .LastIndexOf, and .SubString are what you want to look into using depending on your actual string contents.
    yes thats what ive been trying and I cant get it to work. Replace is not an option as there is other newlines within the string
    Chris

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Remove trailing NewLines from string

    give us an example string as we can not write the code without an example.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    Quote Originally Posted by RobDog888
    give us an example string as we can not write the code without an example.
    take a look at the post before yours
    Chris

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Remove trailing NewLines from string

    There is no space in your string?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    yes your right, i dont need to use a space??? you mean newline? in my test string because i'm not interested in the replace function as that will remove all newlines, for arguments sake lets say the string looks like this then, but there could be more or less newlines at the end

    Code:
    "abc" & vbCrLf & "x zy " & vbcrlf & vbcrlf & "ab c de " & vbcrlf & vbcrlf & vbcrlf
    Chris

  9. #9
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: Remove trailing NewLines from string

    Why not simply loop through your string

    While myString.(mystring.lenth -1) = Vbcrlf
    'Remove Vbcrlf with Substring
    Next

  10. #10

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    ive tried this but it does nothing

    VB Code:
    1. While Data.Substring(Data.Length - 1, 1) = vbCrLf
    2.             Data = Data.Substring(Data.Length - 1)
    3.         End While
    Chris

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Remove trailing NewLines from string

    So you only want one newline and never have two or more of them together, correct?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    Quote Originally Posted by RobDog888
    So you only want one newline and never have two or more of them together, correct?
    no sorry I want to remove all new lines at the end of the string but leaving any spaces intact
    Chris

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Remove trailing NewLines from string

    Newlines are different then spaces. If you want to remove all newlines then Replace is what you want.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    noooo not replace! for it is evil, it will take away all my newlines within the string
    Code:
    "abc" & vbCrLf & "x zy " & vbcrlf & vbcrlf & "ab c de " & vbcrlf & vbcrlf & vbcrlf
    Chris

  15. #15
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: Remove trailing NewLines from string

    You can use Regex.Replace, although I still am not quite sure what you meant by your post just above. Also, Environment.Newline should be used instead of vbcrlf...
    VB Code:
    1. Dim MyLines As String = "this " & Environment.NewLine & Environment.NewLine & _
    2.                                 "and that" & Environment.NewLine & " and another!"
    3. MessageBox.Show(System.Text.RegularExpressions.Regex.Replace(MyLines, Environment.NewLine, ""))
    Last edited by gigemboy; Apr 15th, 2006 at 02:21 PM.

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: Remove trailing NewLines from string

    Ok, now that will need the use of SubString and LastIndexOf.
    VB Code:
    1. Dim str As String = "abc" & vbCrLf & "x zy " & vbCrLf & vbCrLf & "ab c de " & vbCrLf & vbCrLf & vbCrLf
    2. Do While str.LastIndexOf(Environment.NewLine) = str.Length - 2
    3.     str = str.Substring(0, str.LastIndexOf(Environment.NewLine) - 2)
    4. Loop
    5. MessageBox.Show(str)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    thx robdogg but that removes the final two chars of my string which are not newlines, it removes "e " from the end, where all other chars including spaces should remain intact
    Chris

  18. #18
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: Remove trailing NewLines from string

    Try this...Should remove the last Newline, regardless of where it is positioned at...
    VB Code:
    1. Dim MyLines As String = "this " & Environment.NewLine & Environment.NewLine & _
    2.                                       "and that" & Environment.NewLine & " and another!"
    3. MessageBox.Show(MyLines)
    4. Dim MyBeginning As String = MyLines.Substring(0, MyLines.LastIndexOf(Environment.NewLine))
    5. Dim MyEnd As String = MyLines.Substring(MyLines.LastIndexOf(Environment.NewLine) + 2, _
    6.                               MyLines.Length - MyLines.LastIndexOf(Environment.NewLine) - 2)
    7. MessageBox.Show(MyBeginning & MyEnd)
    Of course, you could combine it all into one line, just wanted to make it a little more readable
    Last edited by gigemboy; Apr 15th, 2006 at 04:03 PM.

  19. #19

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    Quote Originally Posted by gigemboy
    Try this...Should remove the last Newline, regardless of where it is positioned at...
    thx gigemboy but what I'm actually looking for is some code which will remove all trailing new lines from the string, but leaving any other char (including spaces) intact
    Chris

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Remove trailing NewLines from string

    This will remove all carriage return and line feed characters from the end of your string:
    VB Code:
    1. myString = myString.TrimEnd(Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed))

  21. #21

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    this is my example string
    Code:
    "abc" & Environment.NewLine & "x zy " & Environment.NewLine & Environment.NewLine & "ab c de " & Environment.NewLine & Environment.NewLine & Environment.NewLine
    I just want to cut off the NewlInes at the end so it will be
    Code:
    "abc" & Environment.NewLine & "x zy " & Environment.NewLine & Environment.NewLine & "ab c de "
    although this is only an example string, the string will be generated programatically so there could be only 1 newline at the end, or maybe 5 if you see what I mean
    Chris

  22. #22
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: Remove trailing NewLines from string

    You can actually do it in a couple lines of code using regex if thats the case, since regex has a handly little "$" character that you can add to only match at the end of the string...
    VB Code:
    1. Dim MyLines As String = "this" & Environment.NewLine & _
    2.                                       "and that" & Environment.NewLine & "and another!" & _
    3.                                       Environment.NewLine & Environment.NewLine
    4.         'just shows you the string so you can compare with the result string
    5.         'to see if it looks the same
    6.         MessageBox.Show(MyLines)
    7.         Dim MyMatches As System.Text.RegularExpressions.MatchCollection = _
    8.                 System.Text.RegularExpressions.Regex.Matches(MyLines, Environment.NewLine)
    9.         'just displays original count of newlines to see if it works
    10.         MessageBox.Show(MyMatches.Count.ToString)
    11.         MyLines = System.Text.RegularExpressions.Regex.Replace(MyLines, "(" & Environment.NewLine & ")*$", "")
    12.         'just results, shows count of newline character sequence in result
    13.         'as well as result string
    14.         Dim MyMatches2 As System.Text.RegularExpressions.MatchCollection = _
    15.                 System.Text.RegularExpressions.Regex.Matches(MyLines, Environment.NewLine)
    16.         MessageBox.Show(MyMatches2.Count.ToString)
    17.         MessageBox.Show(MyLines & " ")
    Last edited by gigemboy; Apr 16th, 2006 at 06:48 AM. Reason: simpler regex pattern

  23. #23

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Remove trailing NewLines from string

    Quote Originally Posted by jmcilhinney
    This will remove all carriage return and line feed characters from the end of your string:
    VB Code:
    1. myString = myString.TrimEnd(Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed))
    this works perfectly, thanks a lot mate

    ps I cant +++++rep you again yet so I will when I can
    Chris

  24. #24
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [RESOLVED] Remove trailing NewLines from string

    well a summary of my above post can be done in one line doing this:
    VB Code:
    1. Dim MyLines As String = "this" & Environment.NewLine & _
    2.                                       "and that" & Environment.NewLine & "and another!" & _
    3.                                       Environment.NewLine & Environment.NewLine
    4. 'removes trailing newlines
    5. MyLines = System.Text.RegularExpressions.Regex.Replace(MyLines, "(" & Environment.NewLine & ")*$", "")
    The rest was just results to make sure you could see to test.. But JM's post was fairly simple
    Last edited by gigemboy; Apr 16th, 2006 at 06:49 AM. Reason: Simpler regex pattern

  25. #25

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: [RESOLVED] Remove trailing NewLines from string

    thx to gigemboy and robdog +rep when I can
    Chris

  26. #26
    New Member
    Join Date
    May 2017
    Posts
    5

    Re: Remove trailing NewLines from string

    I guess everyone want to do it in one line of vba... i like loops.

    bSTR = Trim(bSTR)
    While Right(bSTR, Len(bSTR)) = Asc(10) Or Right(bSTR, Len(bSTR)) = Asc(13)
    bSTR = Left(bSTR, Len(bSTR) - 1)
    Wend

  27. #27
    New Member
    Join Date
    May 2017
    Posts
    5

    Re: [RESOLVED] Remove trailing NewLines from string

    Crud... rushing is never good. But this works:
    While Asc(Right(bSTR, 1)) = 10 Or Asc(Right(bSTR, 1)) = 13
    bSTR = Left(bSTR, Len(bSTR) - 1)
    Debug.Print Len(bSTR)
    Wend

  28. #28
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Remove trailing NewLines from string

    Quote Originally Posted by vb1der View Post
    Crud... rushing is never good. But this works:
    While Asc(Right(bSTR, 1)) = 10 Or Asc(Right(bSTR, 1)) = 13
    bSTR = Left(bSTR, Len(bSTR) - 1)
    Debug.Print Len(bSTR)
    Wend
    This thread is 11.5 years old and it's in the VB.NET forum. Was posting some VBA code really all that useful?

  29. #29
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Re: [RESOLVED] Remove trailing NewLines from string

    Hello from 2021 !

    jmcihinney, I have just created an account for the sole purpose of answering your question.
    I am currently at work, half an hour after the end of my shift and I wanted a quick answer
    on how to quickly trim any and all vbcrlf s at the end or beginning of my string

    I have search google for "VBA trim trailing vbcrlf" and this forum thread is the second result.

    The elegant answer you have given involving myString.TrimEnd, does not work because, yes, this is a vb.net answer.
    Apparently no one asked this question before for VBA or maybe it is in the catacombs on the second page of google's search results.

    So, I'm pretty happy that vb1der has posted a VBA answer.

    And I will now test this answer

    From VB immediate console
    Code:
    bSTR = "mystringblabla" & vbcrlf & vbcrlf & vbclf
    print bSTR
    mystringblabla
    
    
    While Asc(Right(bSTR, 1)) = 10 Or Asc(Right(bSTR, 1)) = 13 : bSTR = Left(bSTR, Len(bSTR) - 1) : Wend
    print bSTR
    mystringblabla
    marker------------------marker
    And I made a function out of it too

    Code:
    Function TrimEnd(ByVal myString As String, myTrim As String) As String
        If myString = "" Or myTrim = "" Then TrimEnd = myString: Exit Function
        While Right(myString, Len(myTrim)) = myTrim: myString = Left(myString, Len(myString) - Len(myTrim)): Wend: TrimEnd = myString
    End Function

  30. #30
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Remove trailing NewLines from string

    Quote Originally Posted by shodanx View Post
    Hello from 2021 !

    jmcihinney, I have just created an account for the sole purpose of answering your question.
    I am currently at work, half an hour after the end of my shift and I wanted a quick answer
    on how to quickly trim any and all vbcrlf s at the end or beginning of my string

    I have search google for "VBA trim trailing vbcrlf" and this forum thread is the second result.

    The elegant answer you have given involving myString.TrimEnd, does not work because, yes, this is a vb.net answer.
    Apparently no one asked this question before for VBA or maybe it is in the catacombs on the second page of google's search results.

    So, I'm pretty happy that vb1der has posted a VBA answer.

    And I will now test this answer

    From VB immediate console
    Code:
    bSTR = "mystringblabla" & vbcrlf & vbcrlf & vbclf
    print bSTR
    mystringblabla
    
    
    While Asc(Right(bSTR, 1)) = 10 Or Asc(Right(bSTR, 1)) = 13 : bSTR = Left(bSTR, Len(bSTR) - 1) : Wend
    print bSTR
    mystringblabla
    marker------------------marker
    And I made a function out of it too

    Code:
    Function TrimEnd(ByVal myString As String, myTrim As String) As String
        If myString = "" Or myTrim = "" Then TrimEnd = myString: Exit Function
        While Right(myString, Len(myTrim)) = myTrim: myString = Left(myString, Len(myString) - Len(myTrim)): Wend: TrimEnd = myString
    End Function
    Does the fact that someone not interested in VB.NET justify polluting the VB.NET forum with code that is of no use to anyone interested in VB.NET? No, it does not. We could pollute this forum with all sorts of other languages and even non-programming information and it might be useful to all sorts of people, but this is a VB.NET forum, so it's purpose is not to help those people. Maybe vb1der should have posted that code to a VBA site somewhere and then it would have been just as useful to you without crapping on this thread and this forum. I'm happy that you found an answer to your question but I don't want to have to wade through all sorts of garbage to get to the VB.NET stuff in this forum and I doubt anyone else does either.

  31. #31
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Re: [RESOLVED] Remove trailing NewLines from string

    I understand that however, this is the most one of the enticing google result for this query.

    And it contains the correct answer for this question.

    The problem is that there is no reliable way to change the google results to point toward the right answer in the right forum section.

    The fact that this thread in particular has 17 thousand views is an indicator that a LOT of people are searching for this answer and hopefully are finding it here.

    here is what the search result page looks from my end. I immediately knew I was on the right track when I saw it !


  32. #32
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Remove trailing NewLines from string

    Quote Originally Posted by shodanx View Post
    I understand that however
    Let me finish that for you:
    I don't care because it helped me so screw what this forum is actually for.
    Have you made the effort to put this information in a logical place for other VBA developers? No, I didn't think so. I guess I'll join a knitting circle and post VBA answers on their message board because who cares what that board is actually for, right? This thread is a question about a VB.NET problem. That is its purpose. As such, an answer using VBA code is, by definition, not useful. The fact that it might be useful for some other purpose isn't relevant to that. Should we start posting Béarnaise sauce recipes here because someone might find that useful? I get that that post was useful for you and probably other VBA developers but that is completely irrelevant to my comment. This thread and this forum exist to help people with VB.NET problems so VBA code is NOT useful and should be posted somewhere that exists to help people with VBA problems. Maybe do that instead of wasting your time telling me that I should be OK with this VB.NET forum being polluted with irrelevant garbage by people who couldn't be bothered posting it in a sensible location because that might take a bit of extra effort on their part.

  33. #33
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,044

    Re: [RESOLVED] Remove trailing NewLines from string

    Quote Originally Posted by shodanx View Post

    And I made a function out of it too

    Code:
    Function TrimEnd(ByVal myString As String, myTrim As String) As String
        If myString = "" Or myTrim = "" Then TrimEnd = myString: Exit Function
        While Right(myString, Len(myTrim)) = myTrim: myString = Left(myString, Len(myString) - Len(myTrim)): Wend: TrimEnd = myString
    End Function
    the Function might work, but ever heard of replace
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  34. #34
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Remove trailing NewLines from string

    Quote Originally Posted by jmcilhinney View Post
    This will remove all carriage return and line feed characters from the end of your string:
    VB Code:
    1. myString = myString.TrimEnd(Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed))
    Quote Originally Posted by the182guy View Post
    this works perfectly, thanks a lot mate

    ps I cant +++++rep you again yet so I will when I can

    @jm… I know this is old but…

    myString = myString.TrimEnd(vbCr, vbLf)


  35. #35
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Remove trailing NewLines from string

    Quote Originally Posted by .paul. View Post
    @jm… I know this is old but…

    myString = myString.TrimEnd(vbCr, vbLf)

    Yeah, I wouldn't do it that way myself any more. I would tend to use this:
    vb.net Code:
    1. myString = myString.TrimEnd(ControlChars.Cr, ControlChars.Lf)
    but it amounts to the same thing. It's the one thing I've learned in the last decade and a half.

  36. #36
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: [RESOLVED] Remove trailing NewLines from string

    Just thought I'd throw in my two pennyworth... I'd've tried:

    Code:
    txt = txt.Replace(vbNewline & vbNewline, vbNewline)

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  37. #37
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] Remove trailing NewLines from string

    Quote Originally Posted by Poppa Mintin View Post
    Just thought I'd throw in my two pennyworth... I'd've tried:

    Code:
    txt = txt.Replace(vbNewline & vbNewline, vbNewline)

    Poppa
    You wasted two pence there. That doesn't remove trailing newline characters

  38. #38
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: [RESOLVED] Remove trailing NewLines from string

    Quote Originally Posted by Poppa Mintin View Post
    Just thought I'd throw in my two pennyworth... I'd've tried:

    Code:
    txt = txt.Replace(vbNewline & vbNewline, vbNewline)
    Poppa
    this is what I used in some applications:
    Code:
    txt = txt.Replace(vbCr, "").Replace(vbLf, "")
    but with the trim.end version, my future code will look more elegant
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

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