How to Remove Newline character from a VB String

Thread Solved
Reply

Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

How to Remove Newline character from a VB String

 
0
  #1
Sep 11th, 2006
Hi,
How do you delete a newline character that is inside a VB string? Apparently Trim removes only spaces.

Thanks in Advance.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Remove Newline character from a VB String

 
0
  #2
Sep 11th, 2006
OKay. I managed to, or at least I think I did, to do it with this.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. SearchString = Replace(SearchString, Chr(13), "")
If anyone has a better solution, or it doesnt do what I want, please correct me. Thanks.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How to Remove Newline character from a VB String

 
1
  #3
Sep 11th, 2006
That is the best way to do it. A couple things I would note on this. One of them, is the use of an empty string in the replace function. I know on small applications, that it doesn't make a serious difference, but an empty string uses memory space (well, more than a null value). A Character holds (I believe it's) 8 bytes. A Null Value holds like 1, or 2. So, in really big VB applications, changing "" to vbnullstring can speed up the app (benchmarks show by 40%) by using vbnullstring instead of "". So, you could redo the function as SearchString = Replace(SearchString, Chr(13), vbnullstring) to perform the same action, and save RAM usage. Again, I know this isn't extremely important, especially with modern machines, but it's good to know, and a nice practice to have.

The other thing, and I'm not sure if this is the intended result or not, but the above code will remove ALL chr(13's) from the string, not just the trailing one (when reading files, it's usually not a problem, since input will separate lines by newlines as it's read in, but a lot of apps that I have built require reading a file (or from a socket) 1 character at a time). An alternative method, if you are just wanting to remove the trailing newline, would be to use the left function SearchString = left(SearchString, Len(SearchString) -1).

I'm sure that the replace function is a fine solution for what you are doing, but I wanted to make sure to note the couple of exceptions.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Remove Newline character from a VB String

 
0
  #4
Sep 11th, 2006
Wow. That was some interesting points you made there. Used the vbNullString, as you adviced. And about the second point, yes I want to remove the trailing newspace. But the thing is this is for a Office Macro. I want to remove the newline from the double clicked text the user selected. Depending on the situation, the double clicked text may or may not have a newline. So I can't always be sure that the last character is a newline. So I will have to stick with the Replace function. Keep any additional advice coming. Much appreciated.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How to Remove Newline character from a VB String

 
1
  #5
Sep 11th, 2006
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. if right(SearchString, 1) = chr(13) or right(SearchString, 1) = chr(10) then
  2. SearchString = right(SearchString, len(SearchString) -1)
  3. end if
Would Also Do The Trick... There's more than one way to strip a newline (ok, that one's bad).
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Remove Newline character from a VB String

 
0
  #6
Sep 11th, 2006
hmmm. Looks like there is a problem with your last snippet. When I select it with a newline, I get a completely different character. Mind you that I am working in Japanese, so maybe the fact that unicode characters have 2 bytes comes into play here. So running the above code with the selected text as


障害NL leaves me with a character like (but not exact) this. I don't even know what character it is. First time i saw something like that. The replace function works fine. :eek:

Edit.

Checked again. It is the second character in the search string. It was difficult to recognise in the debugger. One of the lines was missing because of the apparent low resolution in the debugger so I thought it was a new character.. Japanese can be subtle sometimes.
Last edited by WolfPack; Sep 11th, 2006 at 7:12 pm.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How to Remove Newline character from a VB String

 
1
  #7
Sep 11th, 2006
It's a problem with my code (BAH):
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. if right(SearchString, 1) = chr(13) or right(SearchString, 1) = chr(10) then
  2. ' /* Should be Left, Not Right */
  3. SearchString = left(SearchString, len(SearchString) -1)
  4. end if
Last edited by Comatose; Sep 11th, 2006 at 7:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to Remove Newline character from a VB String

 
0
  #8
Sep 11th, 2006
Oh. I should have looked more closely myself.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: ashok_paswan is an unknown quantity at this point 
Solved Threads: 1
ashok_paswan ashok_paswan is offline Offline
Newbie Poster

Re: How to Remove Newline character from a VB String

 
0
  #9
Mar 23rd, 2007
Originally Posted by WolfPack View Post
Hi,
How do you delete a newline character that is inside a VB string? Apparently Trim removes only spaces.

Thanks in Advance.

Hi,
You can try this and smile.:lol:

Replace(String, vbNewLine, "", , , vbTextCompare)

Thanks
---Ashok Paswan
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1
Reputation: mikex is an unknown quantity at this point 
Solved Threads: 1
mikex mikex is offline Offline
Newbie Poster

Re: How to Remove Newline character from a VB String

 
0
  #10
Mar 26th, 2007
Originally Posted by ashok_paswan View Post
Hi,
You can try this and smile.:lol:

Replace(String, vbNewLine, "", , , vbTextCompare)

Thanks
---Ashok Paswan
Works perfect, thanks alot! :cheesy:
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC