Deleting blanks in a string

D

Diego F.

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?
 
A

Andrew Morton

Diego said:
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
 
M

Martin H.

Hello Andrew, hello Diego,
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
 
A

Andrew Morton

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
 
R

rowe_newsgroups

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
 
A

Andrew Morton

rowe_newsgroups said:
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
 
D

Diego F.

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.
 
A

Andrew Morton

Diego said:
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
 
D

Diego F.

Andrew Morton said:
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.
 
R

Rick

TrimEnd(" "c) RETURNS the trimmed string so you have to assign it to
something

newstr = TrimEnd(" "c)

Rick
 
D

Diego F.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top