|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Deleting blanks in a stringHi 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. Diego F. wrote:
> I have an application that receives a message from a socket in an String.Trim()> 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? Andrew Hello Andrew, hello Diego,
>> I use the System.Text.Encoding.ASCII.GetString method to convert to Trim will only remove leading and trailing blanks. If you want to remove >> 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() 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 Martin H. wrote:
>> Hello Andrew, hello Diego, Diego F. wrote:>> 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 > I have an application that receives a message from a socket in an The OP did specifically refer to the *end* of the array...> array from a certain size. As the array size may be longer that the > message received, the end of the array has blank characters. Andrew > 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 rowe_newsgroups wrote:
>> The OP did specifically refer to the *end* of the array... Of course :-)> > Then wouldn't the answer be to use TrimEnd() instead of Trim()? > > ;-) 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 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. Diego F. wrote:
> I don't understand. Blanks are still there. I use that code So, what is /really/ in the unused portion of the array? Try datos.TrimEnd() > > 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 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
Show quote
Hide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message It doesn't work. I don't know how to remove that. The blanks are always at 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 > 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. 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. > > > Ok, I think it's done. I used TrimEnd(Chr(0)), as 0 was the ASCII code of
the blank character. -- Regards, Diego F.
Process.Start woes in ASP.Net application
System tray icon does not always show when starting app when Windows starts Best method to parse this flat file Suggestions to learn vb.net Datasource question Help.ShowPopup for all Textboxes on Form? If else and what ever :( (Newbie) Access DB Upgrade Help with WMI error message - "The object exporter specified was not found" get DB results from inner join update |
|||||||||||||||||||||||