By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

remove spaces in text?

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
You can use the Replace (http://tinyurl.com/2asj5) function a few times.
s = s.Replace(" ", String.Empty)
You could do this in a loop that checks if there any spaces left, using the
IndexOf function (http://tinyurl.com/3eb5v).

Propably there are some other ways to do this...

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Jan" <ca*****@planet.nl> schreef in bericht
news:2r*****************************@40tude.net...
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

Nov 20 '05 #2

P: n/a
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

Nov 20 '05 #3

P: n/a
VK
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..
Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
Jan
On Sun, 28 Dec 2003 10:42:25 +0100, Cor wrote:
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
Nov 20 '05 #6

P: n/a
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

"Jan" <ca*****@planet.nl> wrote in message
news:2r*****************************@40tude.net...
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

Nov 20 '05 #7

P: n/a
Cor
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

Nov 20 '05 #8

P: n/a
Cor,
Oops! I missed the regex sample in your post, I only saw the StringBuilder
sample, I liked your StringBuilder sample!

Jay

"Cor" <no*@non.com> wrote in message
news:uD**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.