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

What would be the best and easiest way of getting currency rates within SharePoint (Custom Actions) or C# project
Decimal.Parse Issue
Route patterns vs individual routes
Why entity framework isn't implement identity map with unit of work?
How to wrap a thread unsafe class into one that is thread safe without wrapping each member function
Read console output while program is running c#
How to run a for loop after a for loop?
alert box from asp.net 3.5 Code-Behind
Quartz.Net 2.0.1 Trigger
WPF UI Animation Library
Using SqlConnection to execute query in VS 2010 Debugger: Evaluation of expression or statement timed out
Deserializing JSON to List<> is not working in C#
How to add an item to the Powerpont context menu using .net add-in project
Unable to add user controls to a panel dynamically
Callback from main function
Data Structure used for C#'s Dictionary.KeyCollection

Categories

HOME
excel-formula
azure-documentdb
macros
botframework
locale
phantomjs
c++builder
download
ebay
uml-designer
markdown
scapy
samsung-gear-s2
ethereum
chef-recipe
lambda-calculus
responsive
luis
powerpivot
vivado-hls
gpo
project-structure
gomobile
xna
quality-center
aspxgridview
poisson
sasl
mongoose-im
emr
semantic-ui-react
folder
edirectory
overflow
lines
kubectl
microstrategy
tunnel
wunderground
ragel
gstreamer-1.0
libconfig
fisheye
ape-phylo
java-5
api-key
google-chrome-storage
denodo
skype-bots
appcmd
intermediate-language
multi-targeting
try-catch-finally
cardview
therubyracer
racket-student-languages
easy68k
visual-web-developer
class-design
scriptengine
syslog4j
vitamio
django-celery
salesforce-service-cloud
dynamics-nav
angular-promise
gulp-cdnizer
transport
android-filterable
vectordrawable
system.diagnostics
attiny
douglas-peucker
xmla
sony-xperia
teamstudio-unplugged
shunting-yard
javadb
os.walk
system.net
telerik-appbuilder
farpoint-spread
nodeunit
mongoid4
nomachine
code-complete
sygic
nsnumber
typography
usersettings
kademlia
select2-rails
mysql2
ncml
libx264
project-files
lwp
conditional-operator
google-local-search
oaw
chromebug
tracd
scalable

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