Home All Groups Group Topic Archive Search About

Deleting blanks in a string

Author
2 May 2007 11:18 AM
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?

--

Regards,

Diego F.

Author
2 May 2007 11:41 AM
Andrew Morton
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
Author
2 May 2007 12:16 PM
Martin H.
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
Author
2 May 2007 12:48 PM
Andrew Morton
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
Author
2 May 2007 1:07 PM
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
Author
2 May 2007 1:36 PM
Andrew Morton
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
Author
2 May 2007 3:30 PM
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.

--

Regards,

Diego F.
Author
2 May 2007 3:48 PM
Andrew Morton
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
Author
2 May 2007 4:03 PM
Diego F.
Show quote Hide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
news:%23swuDHNjHHA.208@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.
Author
2 May 2007 4:37 PM
Rick
TrimEnd(" "c) RETURNS the trimmed string so you have to assign it to
something

newstr = TrimEnd(" "c)

Rick


Show quoteHide quote
"Diego F." <diego_f***@msn.com> wrote in message
news:euAco7MjHHA.4768@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.
>
>
>
Author
2 May 2007 5:06 PM
Diego F.
Ok, I think it's done. I used TrimEnd(Chr(0)), as 0 was the ASCII code of
the blank character.

--

Regards,

Diego F.