473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

remove spaces in text?

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
8 9242
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: john | last post by:
I am using this to remove everything except the 0-9 and a-z characters, but I also want to remove blanks spaces as well(yes even ones in between words) $strippedtext =...
6
by: careytsui | last post by:
I used replace function, but it doesn't work too good because don't know how many spaces on the field. Do Access have wildcard "+" (one or more)?
4
by: colleen1980 | last post by:
I try to remove the spaces with that method but it still dont work. When i run in the debug mode it still shows spaces. If (bCorpFound) Then strCorp = LTrim(strCorp) strCorp = RTrim(strCorp)...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
3
by: YoungJohn | last post by:
I'm extracting data from a database to create a table. My table includes a text field 'Postcode' for postcodes. Sometimes the extracted postcodes are in the format SL37HY and in other...
1
by: ck | last post by:
What should I program in order to close window and remove a text file as soon as users click on "Abort" button. Thanks.
8
by: uzuria | last post by:
Hi there. I am doing a school project and need to be able to retrieve data from a file and display it in a selectable list box. I originally used a random access file, because I need to select...
5
by: Andrew Wingorodov | last post by:
im made subj like this: inline bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; } str.erase ( std.begin () , std::find_if ( str.begin(), str.end(), isnt_space) );
12
oll3i
by: oll3i | last post by:
How do I remove selected text in javascript? Thank You
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.