c#


String from byte array doesn't get trimmed in C#?


I have a byte array similar to this (16 bytes):
71 77 65 72 74 79 00 00 00 00 00 00 00 00 00 00
I use this to convert it to a string and trim the ending spaces:
ASCIIEncoding.ASCII.GetString(data).Trim();
I get the string fine, however it still has all the ending spaces.
So I get something like "qwerty.........." (where dots are spaces due to StackOverflow).
What am I doing wrong?
I also tried to use .TrimEnd() and to use an UTF8 encoding, but it doesn't change anything.
Thanks in advance :)
You have to do TrimEnd(new char[] { (char)0 }); to fix this. It's not spaces - it's actually null characters that are converted weirdly. I had this issue too.
They're not really spaces:
System.Text.Encoding.ASCII.GetString(byteArray).TrimEnd('\0')
...should do the trick.
-Oisin
Trim by default removes only whitespace, where whitespace is defined by char.IsWhitespace.
'\0' is a control character, not whitespace.
You can specify which characters to trim using the Trim(char[]) overload:
string result = Encoding.ASCII.GetString(data).Trim(new char[] { '\0' });
Why try to create the string first and trim it second? This could add a lot of overhead (if the byte[] is large).
You can specify index and count in the GetString(byte[] bytes, int index, int count) overload.
int count = data.Count(bt => bt != 0); // find the first null
string result = Encoding.ASCII.GetString(data, 0, count); // Get only the characters you want
In powershell, you can do this:
$yourString.TrimEnd(0x00)

Related Links

error in c# when use the loop
How to convert array elements in native iOS (Objective-C to C#)
How to add new items in Gridview through POPUP WINDOW
how to avoid open page already exist on Navigation stack? [duplicate]
Create Entity Framework model based on an existing database in ASP.NET Core
Rotate image in a button
WPF: Updating Database (TwoWay) doesn´t work with Linq-Query
c# How to avoid child form to inherit his mdi parent WindowState property?
Using type FileInfo in WPF XAML
Password encryption in PHP equivalent to C#
Is it possible that a C# method parameter of some kind of int can accept null and can update argument
How to dynamically merge a reactivelist of observables into one observable in ReactiveUI, C#
Cannot implicitly convert type 'app.a1<<string>>' to 'app.a1<<object>>'
What exactly are the resources used when querying a database and why do they need disposing?
RegularExpressionValidator ErrorMessage With Border Overflowing Across Two Lines
How to make custom date validation attribute?

Categories

HOME
cxf
ibm
liquibase
azure-database-mysql
nltk
esxi
sms
rethinkdb
obd-ii
soap-client
google-my-business-api
openbsd
websharper
ng2-charts
chef-recipe
cakephp-2.0
comma
console-application
sha1
facebook-marketing-api
smooth-scrolling
virtuemart
project-reactor
aiml
aws-devicefarm
passbook
caldav
ajaxcontroltoolkit
dev-c++
fibonacci
tcpdf
windowserror
deployment-descriptor
traveling-salesman
jms-topic
ng2-bootstrap
log4net-configuration
snapkit
angularjs-resource
dom4j
jibx
jquery-inputmask
delphi-xe
nshttpurlresponse
gradlew
weld
ack
togglebutton
automapper-4
jsfl
runtime-permissions
gige-sdk
playframework-2.3
activesupport
fckeditor
rmq
disconnect
instaparse
key-value-store
nimbus
camellia
hana-sql-script
app-data
newlib
producer-consumer
biginsights
windows-phone-8-emulator
page-flipping
cloud-storage
stderr
chunks
sony-xperia
qxorm
caption
frisby.js
worklight-appcenter
serializer
rpt
xps
carrier
boofcv
node-amqp
fade
angularjs-select2
azure-pack
qgraphicsscene
nsautoreleasepool
auctex
quit
event-propagation
datasheet
vectorwise
cryptolicensing
lgpl
hotfix

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App