Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing extra spaces, line breaks, etc. 2

Status
Not open for further replies.

marduk813

Programmer
Jul 18, 2002
89
US
I'm writing a small app that will condense plain text copied from the clipboard. To do that, I need to remove extra spaces and linebreaks. It's ok if there's one space between characters, but I would need to change " " to " ", for example.

My first thought is that I would need to parse the text data character by character. If I find a space, I would need to see if the next character was also a space. If so, delete it.

Does that sound right or would there possibly be a faster way to do this? Also, is there a better method if I needed to remove all extra characters except for a-z, A-Z, 0-9, punctuation marks, and single spaces?

TIA,

Jas
 
NOT TESTED:

#1 - Remove extra space:
Dim str as string = YourString
While not str.indexof(" ") = -1
str = str.replcace(" ", " ")
end while

#2 - Remove other chars
Dim a As String = "H$ello @The{r]e!"
Dim s As String = String.Empty

For i As Integer = 0 To a.Length - 1
If CType(a.Chars(i), String).IndexOfAny("!@#$%^&*()_+-=\|/<>`~[]{}") Then
s &= a(i)
End If
Next
MsgBox(s)

* VB2005. The way is simple. Include the character if it is not one of these between db quotes


Hope i helped
 
TipGiver, thanks for your response. I won't be able to test your code until later, but I will definitely try it out.

Would it work better to have a list of all valid characters, or a list of all invalid characters? The text I need to parse is actually the body of an email, and I occasionally see characters other than the ones you listed.
 
Well, after putting this project on hold for a few months, I finally got back to it a few weeks ago. I found a good way of removing extra space characters and the code above works for removing any extra characters, but now my problem is that I can't seem to remove line breaks. (or maybe they're newline characters or carriage returns. no idea)

I've tried using Chr(10) and Chr(13), but instead of removing the characters completely (or replacing them with spaces), I'm left with little ASCII squares. I'm not sure if this is a result of the text coming out of the clipboard or what.

Any ideas?
 
Hello!

As for the squares, they could not be 'calculated' if you are for example displaying the string in a non-multyline textbox. To remove the {linefeed and the carriage} = (newline) return do:

yourString = yourString.Replace(Environment.NewLine, " ")


 
Thanks again TipGiver. I was able to use your new code, as well as some of your old code along with some of my own to remove all the characters I needed to remove and still keep a healthy bunch of normal characters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top