473,659 Members | 3,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9257
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.n et...
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: CollapseMultipl eChars
' 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
CollapseMultipl eChars(ByVal strText As String, ByVal
strSingleChar As String) As String
Dim blnCollapsed As Boolean
Dim intDoubleSpaceP os As Integer

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

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

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

strText = strText.Remove( intDoubleSpaceP os + 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 .CollapseMultip leChars.", 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(B yVal sender As Object, _
ByVal e As System.EventArg s) Handles Button1.Click
Dim sb As New System.Text.Str ingBuilder(Me.T extBox1.Text)
Do While sb.ToString.Ind exOf(" ") > -1
sb.Replace(" ", " ")
Loop
sb.Replace(" ", ";")
Me.TextBox1.Tex t = sb.ToString
End Sub
\\\
Private Sub Button2_Click(B yVal sender As Object, _
ByVal e As System.EventArg s) Handles Button2.Click
Dim re As New System.Text.Reg ularExpressions .Regex(TextBox1 .Text)
TextBox1.Text = re.Replace(Text Box1.Text, " {1,}", ";")
Me.TextBox1.Tex t = TextBox1.Text.T oString
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(B yVal sender As Object, _
ByVal e As System.EventArg s) Handles Button1.Click
Dim sb As New System.Text.Str ingBuilder(Me.T extBox1.Text)
Do While sb.ToString.Ind exOf(" ") > -1
sb.Replace(" ", " ")
Loop
sb.Replace(" ", ";")
Me.TextBox1.Tex t = sb.ToString
End Sub
\\\
Private Sub Button2_Click(B yVal sender As Object, _
ByVal e As System.EventArg s) Handles Button2.Click
Dim re As New System.Text.Reg ularExpressions .Regex(TextBox1 .Text)
TextBox1.Text = re.Replace(Text Box1.Text, " {1,}", ";")
Me.TextBox1.Tex t = TextBox1.Text.T oString
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.Reg ularExpressions .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.Reg ularExpressions .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.Reg ularExpressions .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(inpu t, ";")
input = reader.ReadLine ()
Loop

Hope this helps
Jay

"Jan" <ca*****@planet .nl> wrote in message
news:2r******** *************** ******@40tude.n et...
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******** ******@tk2msftn gp13.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
9219
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 = preg_replace('//i','',$originaltext); Please help. Thx
6
48610
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
4611
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) strCorp = Replace(strCorp, " ", "") ' strCurrentChar = Right(strCorp, 1) ' Do While (strCurrentChar = " ") ' Advance 1 char, from right
15
15617
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 output string. But this was trivial. The other option is to use pointers and shift all the characters after the space by one space to the left. I did this program using pointers and then using array too and I get segmentation fault. What is going...
3
4857
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 instances a gap has been left somewhere in the data e.g. S L37HY or SL3 7HY. I want to validate the postcodes by matching them to a master table which contains postcodes in the format SL37HY i.e. no spaces.
1
1563
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
2024
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 particular fields from a record, but the data is not displaying correctly. Because you must give a field length for each string, my data is displaying with a lot of spaces after it. For example if I display a firstname and surname, instead of "Joe...
5
3599
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
3804
oll3i
by: oll3i | last post by:
How do I remove selected text in javascript? Thank You
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.