473,748 Members | 5,429 Online
Bytes | Software Development & Data Engineering Community
+ 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.Enc oding.ASCII.Get String 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 2811
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.Enc oding.ASCII.Get String 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.Enc oding.ASCII.Get String 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.GetStr ing(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.Enc oding.ASCII.Get String(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.Enc oding.ASCII.Get String(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.Enc oding.ASCII.Get String(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.inv alidwrote in message
news:%2******** *******@TK2MSFT NGP05.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_recibido s = s.Receive(bytes )
datos = System.Text.Enc oding.ASCII.Get String(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.Enc oding.ASCII.Get String(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******** ******@TK2MSFTN GP05.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.Enc oding.ASCII.Get String(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

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

Similar topics

3
296
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 'John Any help is appreciated.
6
6097
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 " string " the leading and trailing blanks are not removed. What am I doing wrong? trimIt( std::string s ) { size_t offset = s.length(); // Trim leading spaces ??? s.erase( 0, s.find_first_not_of( "\t\n" ) );
2
4511
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 test values ('B ') db2 => insert into test values ('C') db2 => select distinct COL1, LENGTH(COL1) from test
2
1655
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 subsequently contains 5 blanks instead of the zero-length string I tried to put into it. This problem occurs regardless of whether I execute the SQL from within Cold Fusion or from within Access as an Access query. The same problem occurs on any non-key...
0
1369
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 form to which the fields of the table are bound. When the form opens, focus is set in the Bound Object Frame (olePhoto). At this stage of development, I am simply pasting bitmap pictures into the frame and saving them. The problem arises when I...
34
4107
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
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4597
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
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 we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.