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

Deleting blanks in a string

P: n/a
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
Share this Question
Share on Google+
10 Replies


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

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

Martin
May 2 '07 #3

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

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

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

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

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

P: n/a

"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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.