remove spaces in text?

  • Thread starter Thread starter Jan
  • Start date Start date
J

Jan

Hi there,

Is there a way to remove spaces from a line of text?

For example:

vr 12-12-79 city village state name

I want it to be:

vr;12-12-79;city;village;state;name

Thx in advance
 
Here's a useful function I wrote just a few weeks ago...


' -----------------------------------------
-------------------------------------
' Function Name: CollapseMultipleChars
' Description: Collapses multiple
blocks of the specified character to a
' single instance of the
specified character.
' Parameters: String to parse and
collapse.
' Character to collapse.
' Returns: String after character
collapsing.
' Author/Date: Steve Randall, 05
December 2003
' -----------------------------------------
-------------------------------------
Public Shared Function
CollapseMultipleChars(ByVal strText As String, ByVal
strSingleChar As String) As String
Dim blnCollapsed As Boolean
Dim intDoubleSpacePos As Integer

Try
If strText.Trim <> "" Then
strText =
strText.Trim
blnCollapsed =
False
Do

intDoubleSpacePos = strText.IndexOf(strSingleChar
& strSingleChar)
If
intDoubleSpacePos <> -1 Then

'Replace the located instance of a double
character with a single character.

strText = strText.Remove(intDoubleSpacePos + 1, 1)
Else

blnCollapsed = True
End If
Loop Until
blnCollapsed
Return strText
Else
Return strText
End If

Catch objException As Exception
Throw New Exception
("Internal error occured in
FunctionLibrary.CollapseMultipleChars.", objException)
End Try
End Function
 
you might also want to do a search on 'split' on msdn.. microsoft has a small function that you can use in your tokenises (is that a word!?) a string given some specified delimiters..
 
Hi Jan,

2 solutions,

The first is the fastest, the second to shortest to write
\\\
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim sb As New System.Text.StringBuilder(Me.TextBox1.Text)
Do While sb.ToString.IndexOf(" ") > -1
sb.Replace(" ", " ")
Loop
sb.Replace(" ", ";")
Me.TextBox1.Text = sb.ToString
End Sub
\\\
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim re As New System.Text.RegularExpressions.Regex(TextBox1.Text)
TextBox1.Text = re.Replace(TextBox1.Text, " {1,}", ";")
Me.TextBox1.Text = TextBox1.Text.ToString
End Sub
///

I hope this helps a little bit?

Cor
 
Hi Jan,

2 solutions,

The first is the fastest, the second to shortest to write
\\\
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim sb As New System.Text.StringBuilder(Me.TextBox1.Text)
Do While sb.ToString.IndexOf(" ") > -1
sb.Replace(" ", " ")
Loop
sb.Replace(" ", ";")
Me.TextBox1.Text = sb.ToString
End Sub
\\\
Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim re As New System.Text.RegularExpressions.Regex(TextBox1.Text)
TextBox1.Text = re.Replace(TextBox1.Text, " {1,}", ";")
Me.TextBox1.Text = TextBox1.Text.ToString
End Sub
///

I hope this helps a little bit?

Cor

Thank you very much,

youre solutions helped me.

JAn
 
Jan,
In addition to the other suggestions, I would look at using the
System.Text.RegularExpressions.Regex.Replace method, searching for white
space, replacing it with a semi-colon.

Something like:
Dim input As String = "vr 12-12-79 city village state
name"
Dim output As String

output = System.Text.RegularExpressions.Regex.Replace(input, "\s+", ";")

Debug.WriteLine(input, "input")
Debug.WriteLine(output, "output")

The "\s+" above is one or more white space characters.

If you are doing multiple of these replacements, I would consider creating
an instance of the RegEx class.

Dim re as New System.Text.RegularExpressions.Regex("\s+")

Dim reader as StreamReader
Dim input As String
Dim output As String

input = reader.ReadLine()
Do Until input Is Nothing
output = re.Replace(input, ";")
input = reader.ReadLine()
Loop

Hope this helps
Jay
 
Hi Jay B,

I special added that regex to my example, but took the wrong one, I now
changed it to this one also

You pointed me before on that,

I changed it I hope that I will give the next time the right one,

:-)

Cor
 
Cor,
Oops! I missed the regex sample in your post, I only saw the StringBuilder
sample, I liked your StringBuilder sample!

Jay
 
Back
Top