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

Programmatically set the DialogResult for SaveFileDialog
Migrating consideration Asp.Net webforms/ MVC website from dedicated to cloud hosting
GMaps.NET: retrieve maps in background
Connect IPv6 client to IPv4 server
Cannot create StorageItem in Outlook Add-In / OutlookSpy decode Hex-value
adding multiple user control node on random location base on observable list
How to play a video from a folder in a different computer
Calling a method with a Dictionary argument
c# xamarin unable to expand the web view using xamarin cross platform project
Binary data being mangled when transfered in string between functions
Azure Function for Bot Framework C#
How to connect VSTO C# code to the ribbon and/or keyboard shortcuts
How to re-execute all commands in a rolled back SqlTransaction instance?
Sorting DataTable column is not working in C#
How to get currently viewed slide in powerpoint vsto add-in when clicked between two slides in slide pane?
Saxon API (saxon9HE) output is not generating proper xml

Categories

HOME
youtube-api
redux
teradata
cxf
snappy
dynamic
conceptual
x86-64
spotfire
where
hiveql
google-docs-api
jshell
docker-compose
servicenow
qt4
soap-client
dcos
spring-data-solr
apache-httpclient-4.x
jetbrains
logstash-configuration
hql
popup
google-closure-compiler
samba
ringcentral
jquery-easyui
geography
project-structure
polybase
dev-c++
infinite-loop
inline-assembly
email-client
aws-iot
bootstrap-accordion
gsuite
chartist.js
sha
mex
angular2-changedetection
bytebuffer
kubectl
webclient
imageprocessor
javax
certbot
asp.net-webhooks
leaderboard
realloc
matrix-multiplication
yelp
mapquest
jslider
jquery-ui-slider
angular-fullstack
qtwebkit
mef
jks
confluent
serialversionuid
runtime-permissions
visualizer
photoshop-script
node.js-client
was
scanf
msiexec
huawei
racket-student-languages
consolidation
strtol
objectify
kik
audiorecord
ooad
videoview
google-app-engine-php
matlab-engine
kramdown
magnet-uri
ruby-1.8.7
transport
properties-file
eaaccessory
data-representation
tidesdk
nexusdb
attiny
register-allocation
javacompiler
msdropdown
dateadd
iwork
dayofweek
infor-eam
kuka-krl
db2-connect
nativequery
autofilter
mysql-error-1146
grunt-wiredep
kademlia
magicsuggest
urlfetch
wxperl
maven-eclipse-plugin
utm
qvariant
lightopenid
auto-compile
response-time
javah
nyromodal
mysql-pconnect
expander
cons

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