473,473 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Deleting blanks in a string

Hi all.

I have an application that receives a message from a socket in an array from
a certain size. As the array size may be longer that the message received,
the end of the array has blank characters.

I use the System.Text.Encoding.ASCII.GetString method to convert to string
and insert in a database. The problem is that in the database the blanks are
inserted as well. How can I remove the blanks before inserting in the
database?

--

Regards,

Diego F.

May 2 '07 #1
10 2789
Diego F. wrote:
I have an application that receives a message from a socket in an
array from a certain size. As the array size may be longer that the
message received, the end of the array has blank characters.

I use the System.Text.Encoding.ASCII.GetString method to convert to
string and insert in a database. The problem is that in the database
the blanks are inserted as well. How can I remove the blanks before
inserting in the database?
String.Trim()

Andrew
May 2 '07 #2
Hello Andrew, hello Diego,
>I use the System.Text.Encoding.ASCII.GetString method to convert to
string and insert in a database. The problem is that in the database
the blanks are inserted as well. How can I remove the blanks before
inserting in the database?

String.Trim()
Trim will only remove leading and trailing blanks. If you want to remove
any blank no matter at which position it is, use Replace instead

Dim s As String = " 12345 67890 "
s = Replace (s, " ", "")

Nach dem Aufruf von Replace hat s den Wert "1234567890".

Beste Grüße,

Martin
May 2 '07 #3
Martin H. wrote:
>Hello Andrew, hello Diego,
Trim will only remove leading and trailing blanks. If you want to
remove any blank no matter at which position it is, use Replace
instead
Diego F. wrote:
I have an application that receives a message from a socket in an
array from a certain size. As the array size may be longer that the
message received, the end of the array has blank characters.
The OP did specifically refer to the *end* of the array...

Andrew
May 2 '07 #4
The OP did specifically refer to the *end* of the array...

Then wouldn't the answer be to use TrimEnd() instead of Trim()?

;-)

Thanks,

Seth Rowe

May 2 '07 #5
rowe_newsgroups wrote:
>The OP did specifically refer to the *end* of the array...

Then wouldn't the answer be to use TrimEnd() instead of Trim()?

;-)
Of course :-)

OP: if you actually know the length of the data, you could use the
Encoding.GetString(Byte[], start as Int32, length as Int32) method overload.

Andrew
May 2 '07 #6
I don't understand. Blanks are still there. I use that code

Dim datos As String
Dim bytes(1999) As Byte
Dim bytes_recibidos As Integer

bytes_recibidos = s.Receive(bytes)
datos = System.Text.Encoding.ASCII.GetString(bytes)
datos.TrimEnd(" "c)
s is a socket object
I write 'datos' in a text file and it appears with blanks at the end, untiil
the total 2000 characters.

--

Regards,

Diego F.

May 2 '07 #7
Diego F. wrote:
I don't understand. Blanks are still there. I use that code

Dim datos As String
Dim bytes(1999) As Byte
Dim bytes_recibidos As Integer

bytes_recibidos = s.Receive(bytes)
datos = System.Text.Encoding.ASCII.GetString(bytes)
datos.TrimEnd(" "c)
So, what is /really/ in the unused portion of the array? Try datos.TrimEnd()
so that it can remove bytes with a value of zero (I hope - the docs don't
say what is regarded as white space), which is not the same as bytes with a
value of 32 (a space).

Or how about

datos = System.Text.Encoding.ASCII.GetString(bytes, 0, bytes_recibidos )

so that you don't get the unwanted data in the first place?

Andrew
May 2 '07 #8

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
Diego F. wrote:
>I don't understand. Blanks are still there. I use that code

Dim datos As String
Dim bytes(1999) As Byte
Dim bytes_recibidos As Integer

bytes_recibidos = s.Receive(bytes)
datos = System.Text.Encoding.ASCII.GetString(bytes)
datos.TrimEnd(" "c)

So, what is /really/ in the unused portion of the array? Try
datos.TrimEnd() so that it can remove bytes with a value of zero (I hope -
the docs don't say what is regarded as white space), which is not the same
as bytes with a value of 32 (a space).

Or how about

datos = System.Text.Encoding.ASCII.GetString(bytes, 0, bytes_recibidos )

so that you don't get the unwanted data in the first place?

Andrew
It doesn't work. I don't know how to remove that. The blanks are always at
the end of the string. I read from a socket, and I tested sending 10 bytes.
When I open the text file there are spaces at the rigth. This should be a
stupid thing, but I can't find the gap.

--

Regards,

Diego F.

May 2 '07 #9
TrimEnd(" "c) RETURNS the trimmed string so you have to assign it to
something

newstr = TrimEnd(" "c)

Rick
"Diego F." <di********@msn.comwrote in message
news:eu**************@TK2MSFTNGP05.phx.gbl...
>I don't understand. Blanks are still there. I use that code

Dim datos As String
Dim bytes(1999) As Byte
Dim bytes_recibidos As Integer

bytes_recibidos = s.Receive(bytes)
datos = System.Text.Encoding.ASCII.GetString(bytes)
datos.TrimEnd(" "c)
s is a socket object
I write 'datos' in a text file and it appears with blanks at the end,
untiil the total 2000 characters.

--

Regards,

Diego F.

May 2 '07 #10
Ok, I think it's done. I used TrimEnd(Chr(0)), as 0 was the ASCII code of
the blank character.

--

Regards,

Diego F.
May 2 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jade | last post by:
hi I want the empty spaces to be removed after a string has been entered by a user i.e how do i get rid of the trailing blanks in the following string.... 'John So that the value now displays...
6
by: jalkadir | last post by:
I am trying to find a way to remove the leading and tailing blanks from a string. What I have come up with is not doing the work as I expected; because if the value I pass to the function is "...
2
by: Laurent | last post by:
DB2 8.1 ------- db2 => create table test (COL1 VARCHAR(10)) db2 => insert into test values ('A') db2 => insert into test values ('A ') db2 => insert into test values ('B') db2 => insert into...
2
by: William Kossack | last post by:
I have an Access table with one primary key and am attempting to update a non-key field, using UPDATE tblMethtest SET fev1timemeth = '' WHERE SID = '0041R'; When I do this, the field...
0
by: Lyn | last post by:
This one is driving me crazy. I have a table which stores pictures in an OLE Object column, one per record with a few other text fields (caption, date, etc). This table is maintained with a...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.