-
[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.
:confused: thanks in advance
-
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.
-
Re: Remove trailing NewLines from string
mystring = mystring.Substring(0, mystring.lastindexof(" "))
?
-
Re: Remove trailing NewLines from string
Quote:
Originally Posted by wossname
mystring = mystring.Substring(0, mystring.lastindexof(" "))
?
no that doesnt work
VB Code:
Data = "abcdefghijkl" & vbNewLine & vbNewLine & vbNewLine
Data = Data.Substring(0, Data.LastIndexOf(" "))
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
-
Re: Remove trailing NewLines from string
give us an example string as we can not write the code without an example. ;)
-
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 ;)
-
Re: Remove trailing NewLines from string
There is no space in your string?
-
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
-
Re: Remove trailing NewLines from string
Why not simply loop through your string
While myString.(mystring.lenth -1) = Vbcrlf
'Remove Vbcrlf with Substring
Next
-
Re: Remove trailing NewLines from string
ive tried this but it does nothing :(
VB Code:
While Data.Substring(Data.Length - 1, 1) = vbCrLf
Data = Data.Substring(Data.Length - 1)
End While
-
Re: Remove trailing NewLines from string
So you only want one newline and never have two or more of them together, correct?
-
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
-
Re: Remove trailing NewLines from string
Newlines are different then spaces. If you want to remove all newlines then Replace is what you want. :confused:
-
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
:)
-
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:
Dim MyLines As String = "this " & Environment.NewLine & Environment.NewLine & _
"and that" & Environment.NewLine & " and another!"
MessageBox.Show(System.Text.RegularExpressions.Regex.Replace(MyLines, Environment.NewLine, ""))
-
Re: Remove trailing NewLines from string
Ok, now that will need the use of SubString and LastIndexOf.
VB Code:
Dim str As String = "abc" & vbCrLf & "x zy " & vbCrLf & vbCrLf & "ab c de " & vbCrLf & vbCrLf & vbCrLf
Do While str.LastIndexOf(Environment.NewLine) = str.Length - 2
str = str.Substring(0, str.LastIndexOf(Environment.NewLine) - 2)
Loop
MessageBox.Show(str)
-
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
-
Re: Remove trailing NewLines from string
Try this...Should remove the last Newline, regardless of where it is positioned at...
VB Code:
Dim MyLines As String = "this " & Environment.NewLine & Environment.NewLine & _
"and that" & Environment.NewLine & " and another!"
MessageBox.Show(MyLines)
Dim MyBeginning As String = MyLines.Substring(0, MyLines.LastIndexOf(Environment.NewLine))
Dim MyEnd As String = MyLines.Substring(MyLines.LastIndexOf(Environment.NewLine) + 2, _
MyLines.Length - MyLines.LastIndexOf(Environment.NewLine) - 2)
MessageBox.Show(MyBeginning & MyEnd)
Of course, you could combine it all into one line, just wanted to make it a little more readable :)
-
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
-
Re: Remove trailing NewLines from string
This will remove all carriage return and line feed characters from the end of your string:
VB Code:
myString = myString.TrimEnd(Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed))
-
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
-
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:
Dim MyLines As String = "this" & Environment.NewLine & _
"and that" & Environment.NewLine & "and another!" & _
Environment.NewLine & Environment.NewLine
'just shows you the string so you can compare with the result string
'to see if it looks the same
MessageBox.Show(MyLines)
Dim MyMatches As System.Text.RegularExpressions.MatchCollection = _
System.Text.RegularExpressions.Regex.Matches(MyLines, Environment.NewLine)
'just displays original count of newlines to see if it works
MessageBox.Show(MyMatches.Count.ToString)
MyLines = System.Text.RegularExpressions.Regex.Replace(MyLines, "(" & Environment.NewLine & ")*$", "")
'just results, shows count of newline character sequence in result
'as well as result string
Dim MyMatches2 As System.Text.RegularExpressions.MatchCollection = _
System.Text.RegularExpressions.Regex.Matches(MyLines, Environment.NewLine)
MessageBox.Show(MyMatches2.Count.ToString)
MessageBox.Show(MyLines & " ")
-
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:
myString = myString.TrimEnd(Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed))
this works perfectly, thanks a lot mate :thumb:
ps I cant +++++rep you again yet so I will when I can :)
-
Re: [RESOLVED] Remove trailing NewLines from string
well a summary of my above post can be done in one line doing this:
VB Code:
Dim MyLines As String = "this" & Environment.NewLine & _
"and that" & Environment.NewLine & "and another!" & _
Environment.NewLine & Environment.NewLine
'removes trailing newlines
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 :)
-
Re: [RESOLVED] Remove trailing NewLines from string
thx to gigemboy and robdog +rep when I can :)
-
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
-
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
-
Re: [RESOLVED] Remove trailing NewLines from string
Quote:
Originally Posted by
vb1der
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?
-
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
-
Re: [RESOLVED] Remove trailing NewLines from string
Quote:
Originally Posted by
shodanx
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.
-
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 !
https://imgur.com/LxCD4Aj
-
Re: [RESOLVED] Remove trailing NewLines from string
Quote:
Originally Posted by
shodanx
I understand that however
Let me finish that for you:
Quote:
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.
-
Re: [RESOLVED] Remove trailing NewLines from string
Quote:
Originally Posted by
shodanx
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
-
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:
myString = myString.TrimEnd(Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed))
Quote:
Originally Posted by
the182guy
this works perfectly, thanks a lot mate :thumb:
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)
:D:D:D
-
Re: Remove trailing NewLines from string
Quote:
Originally Posted by
.paul.
@jm… I know this is old but…
myString = myString.TrimEnd(vbCr, vbLf)
:D:D:D
Yeah, I wouldn't do it that way myself any more. I would tend to use this:
vb.net Code:
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. ;)
-
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
-
Re: [RESOLVED] Remove trailing NewLines from string
Quote:
Originally Posted by
Poppa Mintin
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
-
Re: [RESOLVED] Remove trailing NewLines from string
Quote:
Originally Posted by
Poppa Mintin
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 ;)